#include
#include
#include
#include
using namespace std;
const int N=100010;
int q[N],cnt[N],tt=-1,hh;
vector<int> ver[N];
int n,m;
bool topsort(){
for(int i=1;i<=n;i++)
if(!cnt[i])
q[++tt]=i;
while(tt>=hh)
{
int temp=q[hh++];
for(auto x:ver[temp])
if(--cnt[x]==0)
q[++tt]=x;
}
return tt==n-1;
}
int main(){
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d%d",&n,&m);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
ver[x].push_back(y);
cnt[y]++;
}
if(!topsort()) puts("-1");
else{
for(int i=0;i<=tt;i++)
printf("%d ",q[i]);
puts("");
}
return 0;
}
注意点:缺点:不适用于负环
优点:时间复杂度只与点有关与边无关,适用于稠密图
#include
#include
#include
using namespace std;
const int N=510;
int g[N][N],dist[N];
bool st[N];
int n,m;
int dijkstra(){
memset(dist,0x3f,sizeof dist);
dist[1]=0; //起点
for(int i=0;i<n-1;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
if(!st[j]&&(t==-1||dist[t]>dist[j]))
t=j;
for(int j=1;j<=n;j++)
dist[j]=min(dist[j],dist[t]+g[t][j]);
st[t]=true;
}
if(dist[n]==0x3f3f3f3f) return -1;
else return dist[n];
}
int main(){
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
memset(g,0x3f,sizeof g);
scanf("%d%d",&n,&m);
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
g[x][y]=min(g[x][y],z);
}
int res=dijkstra();
printf("%d\n",res);
return 0;
}
注意点:缺点:不适用于负环
优点:时间复杂度与边有关,适用于稀疏图
#include
#include
#include
#include
#include
#define pii pair
using namespace std;
const int N=100010;
vector<int> ver[N],edge[N];
int dist[N];
bool st[N];
int n,m;
int dijkstra(){
memset(dist,0x3f,sizeof dist);
dist[1]=0;
priority_queue<pii,vector<pii>,greater<pii>> heap;
heap.push({0,1});
while(heap.size())
{
auto temp=heap.top();
heap.pop();
int t=temp.second;
if(st[t]) continue;
st[t]=true;
for(int i=0;i<ver[t].size();i++)
{
int p=ver[t][i],d=edge[t][i];
if(dist[p]>dist[t]+d)
{
dist[p]=dist[t]+d;
heap.push({dist[p],p});
}
}
}
if(dist[n]==0x3f3f3f3f) return -1;
else return dist[n];
}
int main(){
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d%d",&n,&m);
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
ver[x].push_back(y);
edge[x].push_back(z);
}
int res=dijkstra();
printf("%d\n",res);
return 0;
}
缺点:容易被卡,时间复杂度会达到N*M很容易被卡。
优点:可以判负环。
#include
#include
#include
#include
#include
using namespace std;
const int N=100010;
vector<int> ver[N],edge[N];
bool st[N];
int n,m,dist[N];
queue<int> q;
int spfa(){
memset(dist,0x3f,sizeof dist);
st[1]=true;
dist[1]=0;
q.push(1);
while(q.size())
{
int t=q.front();
q.pop();
st[t]=false;
for(int i=0;i<ver[t].size();i++)
{
int p=ver[t][i],d=edge[t][i];
if(dist[p]>dist[t]+d)
{
dist[p]=dist[t]+d;
if(!st[p])
{
q.push(p);
st[p]=true;
}
}
}
}
return dist[n];
}
int main(){
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d%d",&n,&m);
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
ver[x].push_back(y);
edge[x].push_back(z);
}
int res=spfa();
if(res==0x3f3f3f3f) puts("impossible");
else printf("%d\n",res);
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
const int N=2020;
vector<int> ver[N],edge[N];
int n,m;
int dist[N],cnt[N];
bool st[N];
queue<int> q;
bool spfa(){
for(int i=1;i<=n;i++)
{
q.push(i);
st[i]=true;
}
while(q.size())
{
int t=q.front();
q.pop();
st[t]=false;
for(int i=0;i<ver[t].size();i++)
{
int p=ver[t][i],d=edge[t][i];
if(dist[p]>dist[t]+d)
{
dist[p]=dist[t]+d;
cnt[p]=cnt[t]+1;
if(cnt[p]>=n) return true;
if(!st[p])
{
q.push(p);
st[p]=true;
}
}
}
}
return false;
}
int main(){
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d%d",&n,&m);
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
ver[x].push_back(y);
edge[x].push_back(z);
}
if(spfa()) puts("Yes");
else puts("No");
return 0;
}
#include
#include
#include
using namespace std;
const int N=210,INF=0x3f3f3f3f;
int g[N][N],n,m,k;
void floyed(){
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
}
int main(){
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i==j) g[i][j]=0;
else g[i][j]=INF;
}
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
g[x][y]=min(g[x][y],z);
}
floyed();
while(k--)
{
int x,y;
scanf("%d%d",&x,&y);
if(g[x][y]>INF/2) puts("impossible");
else printf("%d\n",g[x][y]);
}
return 0;
}
#include
#include
#include
using namespace std;
const int N=510;
int g[N][N],dist[N],n,m;
bool st[N];
int prim(){
memset(dist,0x3f,sizeof dist);
dist[1]=0;
int res=0;
for(int i=1;i<=n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
if(!st[j]&&(t==-1||dist[t]>dist[j]))
t=j;
if(dist[t]==0x3f3f3f3f) return 0x3f3f3f3f;
res+=dist[t];
for(int j=1;j<=n;j++)
if(!st[j]&&(dist[j]>g[t][j]))
dist[j]=g[t][j];
st[t]=true;
}
return res;
}
int main(){
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
memset(g,0x3f,sizeof g);
scanf("%d%d",&n,&m);
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
g[x][y]=min(g[x][y],z);
g[y][x]=min(g[y][x],z);
}
int res=prim();
if(res==0x3f3f3f3f) puts("impossible");
else printf("%d\n",res);
return 0;
}