Bellman_Ford和Spfa两种方法判断负环
来源:互联网 发布:如何开母婴淘宝店 编辑:程序博客网 时间:2024/05/22 07:50
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;}
因为再次循环的时候有些点的d根本没有被更新,再用这个点去更新别的点的话就浪费时间了。对Bellman_Ford改进,Spfa算法是用一个队列,每次取出一个点,更新和他连接的点,如果被更新的点不在队列中就加入队列中,这样就避免了那个问题。因为是对每个点连接的点操作,这就需要链接表了(可以用vector数组)。还需要一个数组inq判断当前这个点是否在队列里。
如果用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:
- Wormholes are one-way only.
- The time it takes to travel through a wormhole is negligible.
- A wormhole has two end points, each situated in a star system.
- A star system may have more than one wormhole end point within its boundaries.
- For some unknown reason, starting from our solar system, it is always possible to end up in any star system by following a sequence of wormholes (maybe Earth is the centre of the universe).
- Between any pair of star systems, there is at most one wormhole in either direction.
- There are no wormholes with both end points in the same star system.
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.
Input
The input file starts with a line containing the number of cases c to be analysed. Each case starts with a line with two numbersn andm . These indicate the number of star systems () and the number of wormholes () . The star systems are numbered from 0 (our solar system) throughn-1 . For each wormhole a line containing three integer numbersx,y andt is given. These numbers indicate that this wormhole allows someone to travel from the star system numberedx to the star system numberedy, thereby ending upt () years in the future.Output
The output consists of c lines, one line for each case, containing the wordpossible if it is indeed possible to go back in time indefinitely, ornot possible if this is not possible with the given set of star systems and wormholes.Sample Input
23 30 1 10001 2 152 1 -424 40 1 101 2 202 3 303 0 -60
Sample Output
possiblenot 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;}
- Bellman_Ford和Spfa两种方法判断负环
- Bellman_ford和SPFA判断是否存在负环
- Bellman_ford或spfa 判断图是否存在负环
- (POJ 3259)Wormholes 判断负环 bellman_ford 或者 spfa
- Dijkstra/ floyd/floyd扩展求最小环/Bellman_Ford(权值可正可负)/SPFA(权值可正可负)判断负环
- spfa判断负环
- spfa && 判断负环
- poj 3259 (Bellman_Ford判断负环)
- poj3259Wormholes(bellman_ford判断负环)
- poj1860 spfa判断负环
- Wormholes(spfa判断负环)
- 关于spfa 判断负环
- 用SPFA判断负环的方法及其优化
- Bellman_Ford的负环
- 差分约束系统之Bellman_Ford与Spfa判断负权回路
- poj3259( spfa 和 bellman_ford)
- POJ:3259 Wormholes(SPFA判断负环)
- poj3259,简单判断有无负环,spfa
- 约瑟夫环
- “Linux”不等同于Linux内核:构建Linux系统的8个软件
- AdaBoost中利用Haar特征进行人脸识别算法分析与总结
- php preg_replace函数常用例子详解
- 开篇
- Bellman_Ford和Spfa两种方法判断负环
- C语言--结构体
- Visual Studio 2013 新增web项目IIS Express的64位版
- IT15年有些经验,你要提早学会
- 转载-关于C/C++的存储区
- ViewHolder模式超简洁写法,很cool!
- Windows2008上运行java产生8小时时差问题原因
- factory
- IOS 数据存储