标签:amp lse tps describe ble 好的 nbsp run inf
题目: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
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); }
简单的图论思路题 ,,,不过题目挺好的
标签:amp lse tps describe ble 好的 nbsp run inf
原文地址:https://www.cnblogs.com/huangzzz/p/8848221.html