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));//第一遍spfa
memset(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));//第二遍spfa
memset(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;
}

POJ 3268 Silver Cow Party (最短路径)的更多相关文章

  1. POJ 3268 Silver Cow Party 最短路径+矩阵转换

    Silver Cow Party Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) T ...

  2. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  3. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  4. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  6. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  7. poj 3268 Silver Cow Party

                                                                                                       S ...

  8. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  9. 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 ...

随机推荐

  1. python利用beautifulSoup写爬虫

    python BeautifulSoup模块的安装 安装包下载地址:http://www.crummy.com/software/BeautifulSoup/#Download 文档:http://w ...

  2. Bat 批处理杀死进程 重新启动程序

    @echo offset pa=%cd%taskkill /F /IM wgscdTool.exeecho %pa%\wgscdTool.exeping /n 2 127.1>nulstart ...

  3. idea ssm项目迁移到另一台机器上时出现不能正常启动项目的解决方案

    首先右下角提示关联spring文件,关联之,然后启动,发现项目无法启动,然后开始排错 首先从这个日志里发现了这么一条提示信息 然后百度了一下,答案都是说 web.xml 之类的 spring拦截器问题 ...

  4. Yeoman的好基友:Grunt

    grunt介绍 前端不能承受之痛 1.这是我们的生活 文件压缩:YUI Compressor.Google Closure 文件合并:fiddler + qzmin 文件校验:jshint 雪碧图:c ...

  5. Mysql_游标

    MySQL中的游标是一个十分重要的概念.游标提供了一种对从表中检索出的数据进行操作的灵活手段,就本质而言,游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制.MySQL中的游标的语法 ...

  6. 笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)

    初识遗传算法Genetic Algorithm(GA) 遗传算法是计算数学中用于解决最优化的搜索算法,是进化算法的一种.进化算法借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传.突变.自然选 ...

  7. Windos 下python2.7安装 pymssql 解决方案

    最近在学python,到安装pymssql这一块遇到了不少问题. 第一:如何安装python 模块,也是最主要的问题. 可以这么理解:在安装python其它模块之前,可以先安装一个负责安装模块的模块. ...

  8. PAT甲题题解-1071. Speech Patterns (25)-找出现最多的单词

    分割字符串的用法+map映射给出input中出现次数最多的单词,如果次数相同,给出按字典序最小的. 这里我用了自定义分隔符来读取字符串,方法如下: //按照定义的分隔符d来分割字符串,对str进行读取 ...

  9. 对软件工程Alpha迭代的反思与总结

    对软件工程Alpha迭代的反思与总结 本次软件工程的A轮迭代,我们组出了不小的问题.作为一个团队来说,我们的队伍出现了很严重的状况,严重到让老师觉得我们一度失控.于是我撰写此文,借以反思.总结和提高. ...

  10. WC----命令行实现对文件信息的统计

    需求分析: 程序处理用户需求的模式为: wc.exe [parameter][filename] 在[parameter]中,用户通过输入参数与程序交互,需实现的功能如下: 1.基本功能 支持 -c ...