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 ...
随机推荐
- 2017-2018-2 20155230《网络对抗技术》实验9:Web安全基础
实践过程记录 下载wegot并配置好java环境后 输入java -jar webgoat-container-7.0-SNAPSHOT-war-exec.jar 在浏览器输入localhost:80 ...
- RocEDU.课程设计2018 第二周进展 博客补交
本周计划完成的任务 (1).将开发板和平板电脑及其相关配件连通,并和电脑连接. (2).将代码的运行设备从安卓模拟器改为试验箱的平板电脑,平板电脑上实现软件. 本周实际完成情况 (1).计划完成的第一 ...
- tf tensor 输出
在学习TensorFlow的过程中,我们需要知道某个tensor的值是什么,这个很重要,尤其是在debug的时候.也许你会说,这个很容易啊,直接print就可以了.其实不然,print只能打印输出sh ...
- WPF编程,自定义鼠标形状的一种方法。
原文:WPF编程,自定义鼠标形状的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/details/8727 ...
- Luogu P1113 杂务
终于没有打模板题了. 一道简单的拓扑题目(但记得以前第一次做的时候爆0了). 发现这个做事的过程是按一定顺序的,然后如果一个工作的前面没有任何事情的话,它一定先被完成(如果不的话就不能使时间最小化,其 ...
- 【第十二课】FTP服务
目录 FTP服务 1.Linux下部署pure-ftpd 2.FTP的主动和被动模式 2.1.什么是主动FTP 2.2.什么是被动FTP 2.3.主动模式ftp与被动模式FTP优点和缺点: FTP服务 ...
- linux下ipython无法保存历史记录
在Centos7下使用ipython时,发现有个warning,提示无法保存历史记录 [root@localhost pip-]# ipython /usr/local/lib/python3./si ...
- idea git pull项目到本地时容易出现的问题
有时候pull到本地,出了各种错误,其实是因为搞来搞去的,容易出问题,所以最好的方法是拿原有打包好的整个稳定能跑的项目环境, 先git add,然后vcs重置head为hard,然后再pull,一般就 ...
- django请求的生命周期
1. 概述 首先我们知道HTTP请求及服务端响应中传输的所有数据都是字符串. 在Django中,当我们访问一个的url时,会通过路由匹配进入相应的html网页中. Django的请求生命周期是指当用户 ...
- elasticsearch同步mongodb--mongo connector的使用
部署准备 python-3.6.4-amd64.exe mongodb-win32-x86_64-3.4.6-signed.msi (如果已经安装可以忽略) 注意点! 之前我写的一篇文章用的是ela ...