POJ 3268 迪杰斯特拉图论 置换找最短路
Posted 姿态H
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3268 迪杰斯特拉图论 置换找最短路相关的知识,希望对你有一定的参考价值。
题目:https://vjudge.net/problem/POJ-3268
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
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
Sample Input
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Output
10
Hint
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<algorithm> #include<map> #define maxn 200005 #define inf 100000000 using namespace std; int dis[maxn]; int mp[1005][1005]; int n,m,x; int v,u,cost; bool vis[maxn]; int cnt[maxn]; void dijkstra(int n,int v) { bool vis[maxn]; for(int i=1;i<=n;i++) { vis[i]=false; dis[i]=mp[v][i]; } dis[v]=0; vis[v]=true; for(int i=2;i<=n;i++) { int u=v; int mazz=inf; for(int j=1;j<=n;j++){ if(!vis[j]&&dis[j]<mazz)//换dis最小的顶点继续查找 { u=j; mazz=dis[j]; } } vis[u]=true; for(int k=1;k<=n;k++)//更新顶点上的dis { if(!vis[k]&&mp[u][k]<inf) { if(dis[k]>mp[u][k]+dis[u]){ dis[k]=mp[u][k]+dis[u]; } } } } } int main() { scanf("%d%d%d",&n,&m,&x); memset(mp,inf,sizeof(mp)); //memset(mp,inf,sizeof(mp)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) mp[i][j]=inf; for(int i=0;i<m;i++){ cin>>u>>v>>cost; mp[u][v]=cost; } for(int i=1;i<=n;i++) { dis[i]=inf; cnt[i]=inf; } dijkstra(n,x); for(int i=1;i<=n;i++) { cnt[i]=dis[i]; } for(int i=1;i<=n;i++) { dis[i]=inf; } for(int i=1;i<=n;++i) { for(int j=i+1;j<=n;++j) { int aa; aa=mp[j][i]; mp[j][i]=mp[i][j]; mp[i][j]=aa; } } dijkstra(n,x); int ans=0; for(int i=1;i<=n;i++) { if(i!=x); ans=max(ans,dis[i]+cnt[i]); } printf("%d\n",ans); }
简单的图论思路题 ,,,不过题目挺好的
以上是关于POJ 3268 迪杰斯特拉图论 置换找最短路的主要内容,如果未能解决你的问题,请参考以下文章