*最短路径 Bellman-Ford & SPFA 算法实战
2017-08-11 20:11
549 查看
POJ - Wormholes-3259
Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ’s farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Line 1: A single integer, F. F farm descriptions follow.Line 1 of each farm: Three space-separated integers respectively: N, M, and W Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Lines 1..F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).Sample Input
23 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NOYES
Hint
For farm 1, FJ cannot travel back in time.For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
Source
USACO 2006 December Gold题意
题目大意
虫洞问题,现在有n个点,m条边,代表现在可以走的通路,比如从a到b和从b到a需要花费c时间,现在在地上出现了w个虫洞,虫洞的意义就是你从a到b花费的时间是-c(时间倒流,并且虫洞是单向的),现在问你从某个点开始走,能回到从前?解题思路
其实给出了坐标,这个时候就可以构成一张图,然后将“回到从前”理解为“是否会出现负权环”,只要判断是否有负权环就行了。Bellman-Ford算法
解决含负权边的带权有向图的单源最短路径问题 不能处理带负权边的无向图(因可以来回走一条负权边)限制条件: 要求图中不能包含权值总和为负值回路(负权值回路),如下图所示。
最多n-1条边,最多n-1轮就可以实现最短路径,当第n轮路径还在改变,说明此时存在负值回路。
#include<stdio.h> #include<math.h> #include<vector> using namespace std; const int INF=1<<30; struct Edge { int s,e,w; Edge(int ss,int ee,int ww):s(ss),e(ee),w(ww){ } Edge() { } }; vector<Edge> edges;//所有的边 int N,M,W; int dist[1005]; int Bellman_ford(int v) { for(int i=1;i<=N;++i) dist[i]=INF; dist[v]=0; for(int k=1;k<N;++k){//N-1轮松弛 bool flag=false;//对于此题改进操作,若不需要更新了就直接返回假 for(int i=0;i<edges.size();++i){//扫描所有边 int s=edges[i].s; int e=edges[i].e; if(dist[s]+edges[i].w<dist[e]){//松弛 dist[e]=dist[s]+edges[i].w; flag=true; } } if(!flag) return false;//提早结束,不会有负权环 } //再进行1轮松弛,检查有没有负权边,如果出现更短路,则有负权边 for(int i=0;i<edges.size();++i){ int s=edges[i].s; int e=edges[i].e; if(dist[s]+edges[i].w<dist[e]) return true; } return false; } int main() { int T; scanf("%d",&T); while(T--){ edges.clear(); scanf("%d%d%d",&N,&M,&W); for(int i=0;i<M;++i){ int s,e,t; scanf("%d%d%d",&s,&e,&t); edges.push_back(Edge(s,e,t));//双向边等于两条边 edges.push_back(Edge(e,s,t)); } for(int i=0;i<W;++i){ int s,e,t; scanf("%d%d%d",&s,&e,&t); edges.push_back(Edge(s,e,-t)); } if(Bellman_ford(1))//从1可达所有点 printf("YES\n"); else printf("NO\n"); } return 0; }
SPFA算法
SPFA算法是Bellman-Ford算法的改进版,用队列维护dist[]。#include<cstdio> #include<vector> #include<queue> #include<cstring> using namespace std; const int INF=1<<30; struct Edge { int e,w; Edge(int ee,int ww):e(ee),w(ww){ } Edge() { } }; vector<Edge> G[1005]; //整个有向图 int updateTimes[1005];//最短路的改进次数 int dist[1005];//dist[i]是源到i的目前最短路长度 int N,M,W; bool Spfa(int v) { for(int i=1;i<=N;++i)//dis初始化 dist[i]=INF; dist[v]=0;//源点v queue<int> que;//定义队列que que.push(v);//源点v入队列que memset(updateTimes,0,sizeof(updateTimes)); while(!que.empty()){ int s=que.front(); que.pop(); for(int i=0;i<G[s].size();++i){ int e=G[s][i].e; if(dist[e]>dist[s]+G[s][i].w){ dist[e]=dist[s]+G[s][i].w; que.push(e);//没判队列里是否已经有e,可能会慢一些 ++updateTimes[e]; if(updateTimes[e]>=N) return true; } } } return false; } int main() { int T; scanf("%d",&T); while(T--){ scanf("%d%d%d",&N,&M,&W); for(int i=1;i<1000;++i) G[i].clear(); int s,e,t; for(int i=0;i<M;++i){ scanf("%d%d%d",&s,&e,&t); G[s].push_back(Edge(e,t));//双向边等于两条边 G[e].push_back(Edge(s,t)); } for(int i=0;i<W;++i){ scanf("%d%d%d",&s,&e,&t); G[s].push_back(Edge(e,-t)); } if(Spfa(1))//从1可达所有点 printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- 单源最短路径算法 Bellman-Ford && SPFA 及 最短路算法统一归纳
- 单源最短路径算法 Bellman-Ford && SPFA 及 最短路算法统一归纳
- 四种最短路径算法(Dijkstra,Floyd,Bellman-ford&&spfa)
- 单源最短路径算法 Bellman-Ford && SPFA 及 最短路算法统一归纳
- 几大最短路径算法比较(Floyd & Dijkstra & Bellman-Ford & SPFA)
- 单源最短路径:Dijkstra 算法 Bellman_Ford 算法 SPFA 算法
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 总结一下最短路径的贝尔曼-福特算法(Bellman-Ford)及用队列优化(spfa)
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 总结一下最短路径的贝尔曼-福特算法(Bellman-Ford)及用队列优化(spfa)
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 最短路径算法比较(Dijkstra、Bellman-Ford、SPFA)及实现(Java)
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较