最短路(SPFA+负权回路的判断)-poj3268
来源:互联网 发布:网络管理吧 编辑:程序博客网 时间:2024/05/17 23:06
Language:
Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 10887 Accepted: 4831
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
Sample Output
10思路:spfa,关键是去得路不好求,可以把边反向,然后求一次最短路即可。红色部分自己以后注意!!!#include<iostream>#include<set>#include<map>#include<vector>#include<queue>#include<cmath>#include<climits>#include<cstdio>#include<string>#include<cstring>#include<algorithm>typedef long long LL;using namespace std;const int INF=100000000;struct node{ int u,next,time;} cow1[100010],cow2[100010];int pre1[1010],pre2[1010];LL dis1[1010],dis2[1010];bool vis[1010];int cnt[1010];int N,M,X;void spfa(node *cow,LL *dis,int *pre){ for(int i=1; i<=N; i++) { dis[i]=INF; vis[i]=false; } vis[X]=true; dis[X]=0; queue<int> q; memset(cnt,0,sizeof(cnt)); q.push(X); while(!q.empty()) { int t=q.front(); q.pop(); vis[t]=0; for(int j=pre[t]; j!=0; j=cow[j].next) { int v=cow[j].u; if(dis[v]>dis[t]+cow[j].time) { dis[v]=dis[t]+cow[j].time; if(!vis[v]) { vis[v]=true; q.push(v); if(++cnt[v]>N) return; } } } }}int main(){ //freopen("in.txt","r",stdin); int a,b,t; while(cin>>N>>M>>X) { int e=1; memset(pre1,0,sizeof(pre1)); memset(pre2,0,sizeof(pre2)); for(int i=0; i<M; i++) { cin>>a>>b>>t; getchar(); cow1[e].next=pre1[a]; cow1[e].u=b; cow1[e].time=t; cow2[e].next=pre2[b]; cow2[e].u=a; cow2[e].time=t; pre1[a]=pre2[b]=e++; } spfa(cow1,dis1,pre1); spfa(cow2,dis2,pre2); int min1=0; for(int i=1; i<=N; i++) { int m=dis1[i]+dis2[i]; if(min1<m) min1=m; } cout<<min1<<endl; } return 0;}
- 最短路(SPFA+负权回路的判断)-poj3268
- poj 3259 bellman最短路判断有无负权回路
- poj3259Wormholes【最短路SPFA判断负环】
- poj 3259 Wormholes(最短路+spfa+判负回路)
- poj 3259 Wormholes (SPFA 判断有无负权回路)
- POJ 3259Wormholes(SPFA算法判断负权回路)
- POJ 3259 SPFA判断负权回路
- SPFA判断负权回路+uva558
- POJ_3259(Wormholes)(SPFA判断负权回路)
- 【spfa 判断负权回路】POJ
- 最短路之 SPFA(判环+负权)
- poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)
- POJ 3259 Wormholes (最短路 SPFA 判断负环)
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
- bellman-ford算法——最短路问题,判断是否存在负权回路或正权回路
- bellman-ford算法——最短路问题,判断是否存在负权回路或正权回路
- POJ 3259 Wormholes(判断负权回路|SPFA||Bellman-Ford)
- POJ 3295 spfa判断是否存在负权回路
- 20131002组队赛-Regionals 2011, North America - Rocky Mountain
- 在unity3d里,简易又快速的让物体进入Screen
- hibernate配置复合主键
- 【leetcode】Validate Binary Search Tree
- NYOJ -804 Gift (二分)
- 最短路(SPFA+负权回路的判断)-poj3268
- vmware下ubuntu利用虚拟光驱安装vmware tools需注意
- Python 入门教程 14 ---- Practice Makes Perfect
- iOS之同步请求、异步请求、GET请求、POST请求
- android手机速度慢
- 获得数据库的元数据与参数的元数据以及应用
- cf 163e e-Government
- 启动盘启动键
- C++11智能指针之shared_ptr、weak_ptr