问题链接:http://poj.org/problem?id=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?

【输入描述】

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 farm Ai to farm Bi, requiring Ti time units to traverse.

【输出描述】

Line 1: One integer: the maximum of time any one cow must walk.

【样例输入】

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

【样例输出】

10

注意:
题不止求一个点的最短路,

  1. 一个是从牛X的位置返回它们各自的位置的最短距离,我写的是xa函数,这个就是简单的Dijkstra算法的应用
  2. 牛从它们自己的点去牛x的位置,这个有两种方法
    • 以每一个点为起点计算从该点到x的最短距离,如果数据量太大的,就会超时。
    • 将有向边反转,这个和乘以2是不一样的概念,因为图都已经不一样了,可以这么想,在1的情况下,从x返回,现在要从各个顶点去x,那样的路径是不是相当于把边反转以后再从x回来是一样的,把边反转以后,有向图就变了,所以和乘以2不一样。
#include <iostream>
#include <queue>
#include <vector>
#include <cstdio>
#include <algorithm>using namespace std;const int maxn = 1005;
const int inf = 0x3f3f3f3f;struct node
{int to;int cost;node(int t,int c){to = t;cost = c; }friend bool operator < (struct node a,struct node b){return a.cost > b.cost;}
};int d1[maxn];
int d2[maxn];
int vis[maxn];
vector<node> v1[maxn];//从x返回
vector<node> v2[maxn];//去x,将边的反转,即也是从x来求
int n,m,x;void xa()//从x返回
{fill(vis,vis+maxn,0);fill(d1,d1+maxn,inf);priority_queue<node> p;d1[x] = 0;p.push(node(x,0));while(p.empty()!=1){node t = p.top();p.pop();int pos = t.to;if(vis[pos])continue;vis[pos] = 1;for(int i=0;i<v1[pos].size();i++){node x = v1[pos][i];if(!vis[x.to] && d1[x.to]>(d1[pos]+x.cost)){d1[x.to] = d1[pos] + x.cost;p.push(node(x.to,d1[x.to]));}}}
}void ax()//将边反转
{fill(vis,vis+maxn,0);fill(d2,d2+maxn,inf);priority_queue<node> p;d2[x] = 0;p.push(node(x,0));while(p.empty()!=1){node t = p.top();p.pop();int pos = t.to;if(vis[pos])continue;vis[pos] = 1;for(int i=0;i<v2[pos].size();i++){node x = v2[pos][i];if(!vis[x.to] && d2[x.to]>d2[pos]+x.cost){d2[x.to] = d2[pos] + x.cost;p.push(node(x.to,d2[x.to])); }}}
} int main ()
{int i,a,b,c;while(scanf("%d%d%d",&n,&m,&x)!=EOF){for(i=0;i<n;i++){d1[i] = inf;d2[i] = inf;v1[i].clear();v2[i].clear();} for(i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);v1[a].push_back(node(b,c));//去的边 v2[b].push_back(node(a,c));//边的反转 }xa();ax();int ans = -1;for(i=1;i<=n;i++){ans = max(ans,d1[i]+d2[i]);}printf("%d\n",ans);}return 0;
}

下面的代码是没有将边反转,而是以每一个点为起点,计算到x的最短路

#include <iostream>
#include <queue>
#include <vector>
#include <cstdio>
#include <algorithm>using namespace std;const int maxn = 1005;
const int inf = 0x3f3f3f3f;struct node
{int to;int cost;node(int t,int c){to = t;cost = c; }friend bool operator < (struct node a,struct node b){return a.cost > b.cost;}
};int d1[maxn];
int dd[maxn];
int vis[maxn];
vector<node> v1[maxn];//从x返回
int n,m,x;void xa()//从x返回
{fill(vis,vis+maxn,0);fill(d1,d1+maxn,inf);priority_queue<node> p;d1[x] = 0;p.push(node(x,0));while(p.empty()!=1){node t = p.top();p.pop();int pos = t.to;if(vis[pos])continue;vis[pos] = 1;for(int i=0;i<v1[pos].size();i++){node x = v1[pos][i];if(!vis[x.to] && d1[x.to]>(d1[pos]+x.cost)){d1[x.to] = d1[pos] + x.cost;p.push(node(x.to,d1[x.to]));}}}
}int ax(int tem)
{int d2[maxn];fill(vis,vis+maxn,0);fill(d2,d2+maxn,inf);priority_queue<node> p;d2[tem] = 0;p.push(node(tem,0));while(p.empty()!=1){node t = p.top();p.pop();int pos = t.to;if(vis[pos])continue;if(pos==x)return d2[x];vis[pos] = 1;for(int i=0;i<v1[pos].size();i++){node x = v1[pos][i];if(!vis[x.to] && d2[x.to]>d2[pos]+x.cost){d2[x.to] = d2[pos] + x.cost;p.push(node(x.to,d2[x.to])); }}}return d2[x];
} int main ()
{int i,a,b,c;while(scanf("%d%d%d",&n,&m,&x)!=EOF){for(i=0;i<n;i++){d1[i] = inf;v1[i].clear();} for(i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);v1[a].push_back(node(b,c));//去的边 }xa();for(i=1;i<=n;i++){if(i==x)continue;dd[i] = ax(i);}int ans = -1;for(i=1;i<=n;i++){ans = max(ans,d1[i]+dd[i]);}printf("%d\n",ans);}return 0;
}

【POJ】3268 Silver Cow Party (将有向图的边反转)相关推荐

  1. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  2. [POJ](3268)Silver Cow Party ---最短路径(图)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23762   Accepted: 1085 ...

  3. POJ 3268 Silver Cow Party

    题目链接 题意 单向图,N - 1个牛去聚会,求所有牛去聚会和回家路径和的最大值 AC 很骚的操作 首先从派对的地方跑Dijkstra求出回家的最短路,然后将所有边翻转再次从聚会跑Dijkstra就是 ...

  4. POJ - 3268 Silver Cow Party(最短路)

    题目链接:点击查看 题目大意:给出n个点以及m条单项路径和一个点x,设从x点到i的距离及从i回到x点的距离分别为d1和d2,求d1+d2的最大值(1<=i<=n) 题目分析:看到这个题的第 ...

  5. POJ 3268 Silver Cow Party--正反Dijkstra

  6. 【POJ】3268 Silver Cow Party

    题目链接:http://poj.org/problem?id=3268 题意 :有N头奶牛,M条单向路.X奶牛开party,其他奶牛要去它那里.每头奶牛去完X那里还要返回.去回都是走的最短路.现在问这 ...

  7. POJ 3268 D-Silver Cow Party

    http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  8. D - Silver Cow Party POJ - 3268

    D - Silver Cow Party POJ - 3268 dijkstra 是 O(n2),堆优化一下, O(nlogn) 对每个点跑一次 dj, 取 max(dis(x->i)+dis( ...

  9. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

最新文章

  1. 杭州网络推广浅析网站优化如何更快的提升收录?
  2. python培训班 北京-北京python培训班哪家好
  3. windows下redis 和 hiredis的编译与使用
  4. Service Mesh 在超大规模场景下的落地挑战
  5. snb处理器hd3000显卡专用extra_一文看懂显卡的GPU,流处理器,显存,位宽,核心频率是啥?...
  6. JSjQuery全选反选父项子项联动多选框
  7. ACM Doing Homework again
  8. 在ASP.NET MVC3项目中,自定义404错误页面
  9. CVPR2020 | 遮挡也能识别?地平线提出用时序信息提升行人检测准确度
  10. 机器学习预测港股打新收益
  11. ADO.NET数据集的工作原理(DataSet)
  12. vector 内部方法大全 学习(初学者的参考资料)
  13. 相继平均法matlab代码_matlab实现不同平均数的求法
  14. 四个开放源代码审查工具【图文】
  15. 远程服务器网刻系统,无需U盘,网络批量安装系统,pxe网刻工具
  16. Windows系统和Mac OS系统的免费FTP客户端有哪些?
  17. autoconf 版本升级
  18. GIMP的安装和使用
  19. Flutter应用架构之BloC模式实践
  20. 无名师的Unix心传

热门文章

  1. 软件测试:黑盒白盒与动态静态之间有必然联系吗
  2. 语句覆盖(Statement coverage)
  3. Java程序员修炼之路(一)我们为什么选择Java
  4. CSS中连接属性的排序
  5. 基础算法整理(1)——递归与递推
  6. ubuntu bind9 配置简单记录
  7. IAR生产HEX文件
  8. DOS批处理高级教程精选(六)
  9. RDB 和 AOF 持久化的原理是什么?我应该用哪一个?它们的优缺点?
  10. C++关键字static