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. 20155308『网络对抗技术』Exp7:网络欺诈防范

    20155308『网络对抗技术』Exp7:网络欺诈防范 原理与实践说明 1.实践目标 本实践的目标是:理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. 2.实践内容概述 简单应用SET ...

  2. 20155320 EXP8 Web基础

    20155320 EXP8 Web基础 [基础问题回答] 什么是表单? 表单:可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 表单由文本域.复选框.单选框.菜单.文件地址域.按钮等 ...

  3. PYQT5实现控制台显示功能

    首先,写一个信号,用来发射标准输出作为信号 class EmittingStream(QtCore.QObject): textWritten = QtCore.pyqtSignal(str) #定义 ...

  4. Java 利用递归删除文件以及文件夹

    直接上代码: /** * 递归删除 文件/文件夹 * * @param file */ public static void deleteFile(File file) { System.out.pr ...

  5. SpringCloud+Boot简单例子笔记

    一.spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运 ...

  6. (2)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- .NetCore启动配置 和 .NetCoreWebApi

    什么是.Net Core?.Net Core是微软开发的另外一个可以跨Linux.Windows.mac等平台的.Net.Net Core相关知识看文章地步dotnet dllname.dll 运行P ...

  7. stl源码剖析 详细学习笔记 算法(2)

    //---------------------------15/03/29---------------------------- //****************************set相 ...

  8. Microsoft Dynamics CRM 增删改子表汇总子表的某个字段到主表的某个字段(通用插件)

    背景 经常有某个汇总子表的数量到主表的总数量,或者汇总子表的总价到主表的总价这种需求. 传统的做法: 1.就是为每个子表实体单独写成一个插件,但是这样不好复用. 2.主表的汇总字段是汇总货币类型,但是 ...

  9. Docker部署Zookeeper容器

    获取zookeeper镜像 docker pull zookeeper 创建zookeeper容器 docker run --name="zookeeper" -p 2181:21 ...

  10. Nginx浅析

    Nginx浅析 Nginx是什么 总的来说,Nginx其实就是一个和apache类似的服务器软件. Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件代理服务器,并在一个BSD-like ...