Silver Cow Party
来源:互联网 发布:知豆电动汽车代理加盟 编辑:程序博客网 时间:2024/05/17 22:07
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?
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 farmAi 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 farmAi to farm Bi, requiring Ti time units to traverse.
Line 1: One integer: the maximum of time any one cow must walk.
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
10
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
这种题,我们要是直接模拟的思想就是我们先按照题意来建造一张地图,单向的。这样我们就想模拟每一头除x之外的牛来去求最短路径,这样来得到参加宴会的时间,回来的时候就是正常的地图,然后从x直接回家就好了。可是第一种会不会有点没有用,因为我们求了一大顿,只是选择了一个dis[x]来用。所以我们尽量的把所求的都用上,参加宴会的时候,我们是从各个点向x走,我们可不可以反过来,当成是从x向各个点走,也就是反向建图,这样的话,我们只需要求x到各个点的反向距离就好了。#include<stdio.h>#include<string.h>#include<vector>#include<queue>#define INF 0x3f3f3f3fusing namespace std;struct edge{ int v,w;};vector<edge> v1[1010],v2[1010];//下标仅是起点,再取出来int n,m,x;int dis1[1010];int dis2[1010];int vis[1010];void spfa1(int s){ memset(vis,0,sizeof(vis)); memset(dis1,INF,sizeof(dis1)); dis1[s]=0;vis[s]=1; queue<int> q; q.push(s); while(q.size()) { int now=q.front(); q.pop(); vis[now]=0; for(int i=0;i<v1[now].size();i++) { edge temp=v1[now][i];//取出来这条边 int v=temp.v,w=temp.w; if(dis1[v]>dis1[now]+w) { dis1[v]=dis1[now]+w; if(!vis[v]) { vis[v]=1; q.push(v); } } } }}void spfa2(int s){ memset(vis,0,sizeof(vis)); memset(dis2,INF,sizeof(dis2)); dis2[s]=0;vis[s]=1; queue<int> q; q.push(s); while(q.size()) { int now=q.front(); q.pop(); vis[now]=0; for(int i=0;i<v2[now].size();i++) { edge temp=v2[now][i];//取出来这条边 int v=temp.v,w=temp.w; if(dis2[v]>dis2[now]+w) { dis2[v]=dis2[now]+w; if(!vis[v]) { vis[v]=1; q.push(v); } } } }}int main(){ scanf("%d %d %d",&n,&m,&x); for(int i=0;i<10;i++) v1[i].clear(),v2[i].clear(); for(int i=0;i<m;i++) { int u,v,w; edge e; scanf("%d %d %d",&u,&v,&w); e.v=v,e.w=w; v1[u].push_back(e);//正向存图,找到奶牛回去的路,都是从x点开始 e.v=u,e.w=w; v2[v].push_back(e);//反向存图,找到奶牛来的路 } spfa1(x); spfa2(x); int ans=0; for(int i=1;i<=n;i++) if(dis1[i]+dis2[i]>ans) ans=dis1[i]+dis2[i]; printf("%d\n",ans); return 0;}
阅读全文
0 0
- Silver Cow Party
- POJ3268:Silver Cow Party
- poj3268 - Silver Cow Party
- POJ3268 Silver Cow Party
- POJ3268 Silver Cow Party
- Silver Cow Party
- poj_3268 Silver Cow Party
- POJ3268 Silver Cow Party
- K - Silver Cow Party
- poj3268-Silver Cow Party
- Silver Cow Party
- Silver Cow Party
- Silver Cow Party
- poj Silver Cow Party
- POJ3268-Silver Cow Party
- poj Silver Cow Party
- poj3268 Silver Cow Party
- Silver Cow Party
- nobone-sync ......js后watched一次后自动终止的原因
- 银牛派对
- 批量添加手机联系人 | csv/excel转vcf
- android 设置背景透明度
- 浅谈几大最短路
- Silver Cow Party
- HDU 6070 Dirt Ratio 二分+线段树
- Android学习笔记——LitePal
- Java类的创建及类与对象的关系
- C#之入门总结_循环及打印图形_03
- 控制台输入三条边长,判断是否能构成三角形。如果能构成,输出三角形的周长,并且告知该三角形是什么类型(锐角、直角、钝角),再判断下该三角形是 等腰、等边还是不等边三角形。
- Android-Iconics 开源库的使用
- 8月面试的几道编程基础题(持续更新各种面试题)
- 二分-UVA10341