POJ3268 解题报告
来源:互联网 发布:sql server 2008 出错 编辑:程序博客网 时间:2024/06/06 01:28
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 ofM (1 ≤M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi requiresTi (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: Linei+1 describes roadi with three space-separated integers:Ai,Bi, andTi. The described road runs from farmAi to farmBi, 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<algorithm>#include<iostream>#include<cstring>using namespace std;const int INF=0x3f3f3f3f;const int maxn=1000+100;const int maxm=100000+100;int d[maxn][maxn];int N,M,X;void warshall();void solve();void show();int main(){ while(scanf("%d%d%d",&N,&M,&X)==3) { memset(d,0x3f,sizeof(d)); for(int i=0;i<=N;i++) d[i][i]=0; for(int i=0;i<M;i++) { int a,b,t; scanf("%d%d%d",&a,&b,&t); d[a][b]=t; } warshall(); solve(); //show(); } return 0;}void warshall(){ for(int k=1;k<=N;k++) for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]);}void solve(){ int ans=0; for(int i=1;i<=N;i++) if(i!=X) ans=max(ans,d[i][X]+d[X][i]); printf("%d\n",ans);}void show(){ for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) if(d[i][j]==INF) printf("%4s","inf"); else printf("%4d",d[i][j]); printf("\n"); }}
#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<cstdio>using namespace std;struct edge{ int to; int cost;};typedef pair<int,int> P;const int INF=2e9;const int maxn=1000+10;const int maxm=100000+100;vector<edge> G1[maxn],G2[maxn];int d1[maxn],d2[maxn];int N,M,X;void dijkstra1(int s);void dijkstra2(int s);void solve();void show();int main(){ while(cin>>N>>M>>X) { for(int i=0;i<=N;i++) { G1[i].clear(); G2[i].clear(); } for(int i=0;i<M;i++) { int a,b,t; edge e1,e2; cin>>a>>b>>t; e1.to=b; e2.to=a; e1.cost=e2.cost=t; G1[a].push_back(e1); G2[b].push_back(e2); } dijkstra1(X); dijkstra2(X); solve(); //show(); } return 0;}void dijkstra1(int s){ fill(d1,d1+maxn,INF); d1[s]=0; priority_queue<P,vector<P>,greater<P>> que; que.push(P(0,s)); while(!que.empty()) { P p=que.top(); que.pop(); int v=p.second; if(d1[v]<p.first) continue; for(int i=0;i<G1[v].size();i++) { edge e=G1[v][i]; if(d1[e.to]>d1[v]+e.cost) { d1[e.to]=d1[v]+e.cost; que.push(P(d1[e.to],e.to)); } } }}void dijkstra2(int s){ fill(d2,d2+maxn,INF); d2[s]=0; priority_queue<P,vector<P>,greater<P>> que; que.push(P(0,s)); while(!que.empty()) { P p=que.top(); que.pop(); int v=p.second; if(d2[v]<p.first) continue; for(int i=0;i<G2[v].size();i++) { edge e=G2[v][i]; if(d2[e.to]>d2[v]+e.cost) { d2[e.to]=d2[v]+e.cost; que.push(P(d2[e.to],e.to)); } } }}void solve(){ int ans=0; for(int i=1;i<=N;i++) ans=max(ans,d1[i]+d2[i]); cout<<ans<<endl;}void show(){ for(int i=1;i<=N;i++) cout<<d1[i]<<" "; cout<<endl; for(int i=1;i<=N;i++) cout<<d2[i]<<" "; cout<<endl;}
- POJ3268 解题报告
- Dijkstra POJ3268 Silver Cow Party解题报告
- poj3268
- poj3268
- poj3268
- poj3268
- POJ3268
- poj3268
- poj3268
- poj3268
- POJ3268
- poj3268
- poj3268
- poj3268
- poj3268
- POJ3268
- poj3268
- poj3268
- 文章标题
- String类的获取功能:
- SocketChannel Timeout——TCP半开链接
- spring学习笔记1
- ubuntu 彻底卸载apache2
- POJ3268 解题报告
- 51Nod
- python 处理列表元素被赋值汉字
- bingo培训——需求评审一些建议
- 二进制中1的个数
- 牛客网-贪心-扫描透镜
- Hadoop之伪分布式配置
- $compile编译html(让动态生成的html上的ng事件生效)
- adb命令模拟按键事件