POJ 3268 Silver Cow Party
来源:互联网 发布:万达网络科技集团招聘 编辑:程序博客网 时间:2024/04/30 10:07
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; roadi 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, andTi. The described road runs from farmAi to farm Bi, requiringTi time units to traverse.
Output
Sample Input
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
Sample Output
10
Hint
Source
#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <cstring>#include <stdlib.h>using namespace std;int mp[1005][1005];int dis[1005];bool vis[1005];int dis2[1005];const int INF=100000009;int n;void Dijstra(int v){ int minx,x; for(int i=0;i<=n;i++){ dis[i]=mp[v][i]; vis[i]=0; } vis[v]=1; dis[v]=0; for(int i=1;i<n;i++){ minx=INF; for(int j=1;j<=n;j++){ if(minx>dis[j]&&!vis[j]){ minx=dis[j]; x=j; } } if(minx==INF) break; vis[x]=1; for(int j=1;j<=n;j++){ if(!vis[j]&&dis[x]+mp[x][j]<dis[j]) dis[j]=dis[x]+mp[x][j]; } }}void Dijstra2(int v){ int minx,x; for(int i=0;i<=n;i++){ dis2[i]=mp[i][v]; vis[i]=0; } vis[v]=1; dis2[v]=0; for(int i=1;i<n;i++){ minx=INF; for(int j=1;j<=n;j++){ if(minx>dis2[j]&&!vis[j]){ minx=dis2[j]; x=j; } } if(minx==INF) break; vis[x]=1; for(int j=1;j<=n;j++){ if(!vis[j]&&dis2[x]+mp[j][x]<dis2[j]) dis2[j]=dis2[x]+mp[j][x]; } }}int main(){ int m,x; scanf("%d%d%d",&n,&m,&x); memset(mp,INF,sizeof(mp)); for(int i=0;i<m;i++){ int s,e,t; scanf("%d%d%d",&s,&e,&t); if(mp[s][e]>t) mp[s][e]=t; } Dijstra(x); Dijstra2(x); int ans=-INF; for(int i=1;i<=n;i++){ if(i==x) continue; if(dis2[i]==INF||dis[i]==INF) continue; if(ans<dis[i]+dis2[i]) ans=dis2[i]+dis[i]; } printf("%d\n",ans); return 0;}
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ-3268-Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- POJ-3268-Silver Cow Party
- poj 3268--Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ-3268 Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- Android消息处理机制
- HDU-1114-Piggy-Bank (背包)
- XML之行
- NYOJ998---Sum
- tomcat 不能识别maven web项目
- POJ 3268 Silver Cow Party
- AJAX入门---五步使用XMLHttpRequest对象
- Java语言快速入门·简答T
- uva 531 - Compromise
- REST Web 服务介绍 JAX-RS规范和其实现RESTEasy
- HTML 入门教程
- ios沙盒机制
- jdk与jre的区别
- 【BZOJ】【P3674】【可持久化并查集加强版】【题解】【可持久化数组】