POJ 3268 Silver Cow Party (最短路径)
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 (最短路径)的更多相关文章
- POJ 3268 Silver Cow Party 最短路径+矩阵转换
Silver Cow Party Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) T ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
- poj 3268 Silver Cow Party
S ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
随机推荐
- 小兔博客新增源码下载模块,JavaWeb项目实战,JavaScript入门教程 ,JavaSE案例等
从今以后,所有的源码在 http://www.xiaotublog.com/downloadView.html 都可以免费下载,在下载页面还可以直接链接到相关的教程地址(如果有教程的话...). 最近 ...
- flask+socketio+echarts3 服务器监控程序(基于后端数据推送)
本文地址:http://www.cnblogs.com/hhh5460/p/7397006.html 说明 以前的那个例子的思路是后端监控数据存入数据库:前端ajax定时查询数据库. 这几天在看web ...
- 为什么要进行阿里云云计算助理工程师认证(ACA)
阿里云助理工程师认证(ACA - Alibaba Cloud Certification Associate)是面向使用阿里云基础产品的专业技术认证,主要涉及阿里云的计算.存储.网络.安全类的核心产品 ...
- GIT命令基本使用
记录摘选自廖雪峰的官方网站归纳总结 1.centos下安装git [root@cdw-lj ~]# yum install git 2.配置用户名以及邮箱 [root@cdw-lj opt]# git ...
- opencv配置(转)
1. 下载安装Opencv,去官网http://opencv.org/即可下载最新版本的Opencv,此处用的是Opencv 2.4.10 安装时傻瓜式的,最新版本的安装就是相当于解压到你指定的安装目 ...
- Spring+SpringMVC+MyBatis整合基础篇(三)搭建步骤
作者:13GitHub:https://github.com/ZHENFENG13版权声明:本文为原创文章,未经允许不得转载. 框架介绍 Spring SpringMVC MyBatis easyUI ...
- [转载]windows下PHP + Nginx curl访问本地地址超时卡死问题的解决方案
原因: windows 下 nginx+php环境,不支持并发. 解决方案: 1.在配置nginx vhost时,需要同时运行的网站设置不同的fastcgi_pass的端口号 server { ser ...
- 【拾遗】理解Javascript中的Arguments
前言 最近在看JavaScript相关的知识点,看到了老外的一本Javascript For Web Developers,遇到了一个知识盲点,觉得老外写的很明白很透彻,记录下来加深印象,下面是我摘出 ...
- python的闭包函数
在一个外函数中定义了一个内函数,内函数里运用了外函数的临时变量,并且外函数的返回值是内函数的引用.这样就构成了一个闭包. #闭包函数的实例# outer是外部函数 a和b都是外函数的临时变量def o ...
- 机器学习中几种优化算法的比较(SGD、Momentum、RMSProp、Adam)
有关各种优化算法的详细算法流程和公式可以参考[这篇blog],讲解比较清晰,这里说一下自己对他们之间关系的理解. BGD 与 SGD 首先,最简单的 BGD 以整个训练集的梯度和作为更新方向,缺点是速 ...