Bellman_Ford是通过N-1次循环求出每个点到原点的最短路的,每次循环遍历所有的边,如果能更新就更新。前面说过为什么N-1次就行(若一个点到起点的最短路需要经过N个点,包括他自己,那么第N次循环就能确定他的最短路)。
最短路一定是不含环的,如果存在负环就根本不存在最短路了。所以这个方法可以用来判断是否有负环,如果循环了N-1次后还能进行松弛操作,说明有负环。Bellman_Ford的复杂度是O(M*N)。
int bellman(){ //G条边 int i,j,k; memset(d,INF,sizeof(d)); d[0]=0; for(i=0;i<N-1;i++){ for(j=0;j<G;j++){ int u=e[j].u,v=e[j].v; if(d[u]!=INF&&d[u]+w[u][v]<d[v]) d[v]=d[u]+w[u][v]; } } for(j=0;j<G;j++){ int u=e[j].u,v=e[j].v; if(d[u]!=INF&&d[u]+w[u][v]<d[v]) return 1; } return 0; }
如果用spfa判断是否有负环问题,其实原理也是一个点不能被更新N-1次以上,所以再开一个数组记录每个点被加入队列的次数。在网上看说是一个点被加入N次以上就说明有负环,想了半天为什么是N次以上。。。后来发现因为这是有特殊情况的,N=1,这个点被加入一次,但是没负环。。。所以只要判断这个特殊情况,后面条件写成N-1次以上也是对的。。当然为了方便,直接写成N次以上更好。。网上说这个期望时间复杂度O(2*e),这个我就不知道怎么算的了。。。
int spfa(){ queue<int> q; memset(d,INF,sizeof(d)); memset(inq,0,sizeof(inq)); memset(cnt,0,sizeof(cnt)); d[0]=0; inq[0]=1; q.push(0); while(!q.empty()){ int x=q.front(); q.pop(); inq[x]=0; cnt[x]++; if(cnt[x]>N) return 1; if(d[x]==INF) continue; for(int i=0;i<v[x].size();i++){ int y=v[x][i].y; if(d[x]+v[x][i].t<d[y]){ d[y]=d[x]+v[x][i].t; inq[y]=1; q.push(y); } } } return 0; }
Wormholes |
In the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through space and time connecting two star systems. Wormholes have a few peculiar properties:
All wormholes have a constant time difference between their end points. For example, a specific wormhole may cause the person travelling through it to end up 15 years in the future. Another wormhole may cause the person to end up 42 years in the past.
A brilliant physicist, living on earth, wants to use wormholes to study the Big Bang. Since warp drive has not been invented yet, it is not possible for her to travel from one star system to another one directly. Thiscan be done using wormholes, of course.
The scientist wants to reach a cycle of wormholes somewhere in the universe that causes her to end up in the past. By travelling along this cycle a lot of times, the scientist is able to go back as far in time as necessary to reach the beginning of the universe and see the Big Bang with her own eyes. Write a program to find out whether such a cycle exists.
2 3 3 0 1 1000 1 2 15 2 1 -42 4 4 0 1 10 1 2 20 2 3 30 3 0 -60
possible not possible
#include<cstring> #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<queue> #include<map> #include<vector> #define INF 0x3f3f3f3f #define MAXN 1010 #define pii pair<int,int> using namespace std; int N,M,G,w[MAXN][MAXN],d[MAXN],inq[MAXN],cnt[MAXN]; struct edge{ int y,t; edge(int a,int b){ y=a; t=b; } }; vector<edge> v[MAXN]; struct Bellman_edge{ int u,v,t; Bellman_edge(){} Bellman_edge(int a,int b,int d){ u=a; v=b; t=d; } }e[2*MAXN]; int bellman(){ //G条边 int i,j,k; memset(d,INF,sizeof(d)); d[0]=0; for(i=0;i<N-1;i++){ for(j=0;j<G;j++){ int u=e[j].u,v=e[j].v; if(d[u]!=INF&&d[u]+w[u][v]<d[v]) d[v]=d[u]+w[u][v]; } } for(j=0;j<G;j++){ int u=e[j].u,v=e[j].v; if(d[u]!=INF&&d[u]+w[u][v]<d[v]) return 1; } return 0; } int spfa(){ queue<int> q; memset(d,INF,sizeof(d)); memset(inq,0,sizeof(inq)); memset(cnt,0,sizeof(cnt)); d[0]=0; inq[0]=1; q.push(0); while(!q.empty()){ int x=q.front(); q.pop(); inq[x]=0; cnt[x]++; if(cnt[x]>N) return 1; if(d[x]==INF) continue; for(int i=0;i<v[x].size();i++){ int y=v[x][i].y; if(d[x]+v[x][i].t<d[y]){ d[y]=d[x]+v[x][i].t; inq[y]=1; q.push(y); } } } return 0; } int main(){ freopen("in.txt","r",stdin); int T; scanf("%d",&T); while(T--){ scanf("%d%d",&N,&M); memset(w,INF,sizeof(w)); int i,a,b,t; G=0; for(i=0;i<N;i++) v[i].clear(); for(i=0;i<N;i++) w[i][i]=0; for(i=0;i<M;i++){ scanf("%d%d%d",&a,&b,&t); w[a][b]=t; //bellman e[G++]=Bellman_edge(a,b,t); v[a].push_back(edge(b,t)); //spfa } if(bellman()) printf("possible\n"); else printf("not possible\n"); } return 0; }