POJ 3268 Silver Cow Party (最短路径)

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; 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

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.

Output

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

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

Http

POJ:https://vjudge.net/problem/POJ-3268

Source

最短路径

题目大意

在一个有向图中,求所有点都走到一个点再走回来的最短距离中的最大值

解决思路

我们知道单源最短路的求法,即从一个点走到其他点,那么我们只要把有向图中的边反过来求一遍就是从其他点走到一个点的最短距离
这里我们用spfa解决

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;const int maxN=1001;
const int inf=2147483647;class Edge
{
public:int v,w;
};int n,m,X;
vector<Edge> E1[maxN];
vector<Edge> E2[maxN];
queue<int> Q;
bool inqueue[maxN];
int Dist1[maxN];
int Dist2[maxN];int main()
{scanf("%d%d%d",&n,&m,&X);for (int i=1;i<=m;i++){int u,v,w;scanf("%d%d%D",&u,&v,&w);E1[u].push_back((Edge){v,w});//存正图E2[v].push_back((Edge){u,w});//存反图}memset(Dist1,127,sizeof(Dist1));//第一遍spfamemset(inqueue,0,sizeof(inqueue));Dist1[X]=0;inqueue[X]=1;while (!Q.empty())Q.pop();Q.push(X);do{int u=Q.front();Q.pop();inqueue[u]=0;for (int i=0;i<E1[u].size();i++){int v=E1[u][i].v;int w=E1[u][i].w;if (Dist1[v]>Dist1[u]+w){Dist1[v]=Dist1[u]+w;if (inqueue[v]==0){Q.push(v);inqueue[v]=1;}}}}while (!Q.empty());memset(Dist2,127,sizeof(Dist2));//第二遍spfamemset(inqueue,0,sizeof(inqueue));Dist2[X]=0;inqueue[X]=1;Q.push(X);do{int u=Q.front();Q.pop();inqueue[u]=0;for (int i=0;i<E2[u].size();i++){int v=E2[u][i].v;int w=E2[u][i].w;if (Dist2[v]>Dist2[u]+w){Dist2[v]=Dist2[u]+w;if (inqueue[v]==0){Q.push(v);inqueue[v]=1;}}}}while (!Q.empty());int Ans=0;for (int i=1;i<=n;i++)Ans=max(Ans,Dist1[i]+Dist2[i]);//统计最大值cout<<Ans<<endl;return 0;
}

转载于:https://www.cnblogs.com/SYCstudio/p/7225202.html

POJ 3268 Silver Cow Party (最短路径)相关推荐

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

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

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

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

  3. POJ 3268 Silver Cow Party

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

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

  5. 【POJ】3268 Silver Cow Party (将有向图的边反转)

    问题链接:http://poj.org/problem?id=3268 [问题描述] One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  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. POJ3268 Silver Cow Party(最短路径)

    题意: 每个农场有一头牛,现在要到农场x开派对,路径是单向的,要求开完派对后还要回到原农场,求所有牛的最短路径的最大值 要点: 就是一个定终点求最短路径的变形,现在已经知道终点,从终点回到原农场的最短 ...

  9. POJ 3268:Silver Cow Party 求单点的来回最短路径

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15989   Accepted: 7303 ...

最新文章

  1. python序列化模块json和pickle
  2. WINCE6.0下开始菜单的“挂起(suspend)”是否可见及阻止系统进入睡眠模式
  3. 挂载失败-日志中显示僵尸pod的问题
  4. 【数字信号处理】基于DFT的滤波系列3之插值滤波(含MATLAB代码)
  5. 比较TFS与SVN,你必须知道的10点区别
  6. C/C++ _strlwr_s 函数 – 字符串大写转小写- C语言零基础入门教程
  7. 内核并发控制---顺序锁 (来自网易)
  8. mysql基础入门(参照b站黑马程序员整理)
  9. JAVAWeb汽车销售管理系统
  10. STEP7-Microwin SMART软件彻底卸载步骤
  11. 电子词典系统vc++_《VC++ 编程词典(珍藏版)》
  12. Kali系统2022VM版本的安装
  13. 火狐 dns_如何在Firefox中通过HTTPS启用DNS
  14. 上海高中开设计算机课,如何提升高中计算机课的趣味性
  15. 程序员创业:小程序开发费用报价表,包含项目工期和费用明细
  16. 预编译及预处理的理解
  17. sql——如何将html代码存入数据库中
  18. 计算机网络学习通习题
  19. 靠死腾讯,QQ2009不能安装也不能删……
  20. 判定(半)正定矩阵的特殊大于(等于)简写符号

热门文章

  1. C++期末实践程序设计与数组作为参数的注意事项
  2. 2018.9.15,Matlab实验三:字符串、单元数组和结构体
  3. sigkill mysql_Ubuntu不能停止mysqld
  4. 条令考试小程序辅助器_可以自己编题的答题软件,自定义题库考试出题工具,微信答题小程序...
  5. python变量类型是动态的_python内存动态分配过程详解
  6. asr语音转写_搜狗智能录音笔C1正式上市 语音转文字准确率达95%
  7. Oracle 数据怎么实时同步到 SQL Server | 亲测干货分享建议收藏
  8. 如何发布接口_Devops下的接口全生命周期管理与测试
  9. C语言带参宏定义和函数的区别
  10. python的包文件叫什么_python之包和文件目录规范