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 ...
随机推荐
- Sqlite 快速批量插入数据 测试
public static int insertDbBatch() { string sql = ""; SQLiteConnection conn = new SQLiteCon ...
- 20155202《网络对抗》Exp9 web安全基础实践
20155202<网络对抗>Exp9 web安全基础实践 实验前回答问题 (1)SQL注入攻击原理,如何防御 SQL注入产生的原因,和栈溢出.XSS等很多其他的攻击方法类似,就是未经检查或 ...
- 解决 idea 中的 tomcat控制台 和cmd tomcat下的中文乱码问题(win10 64位)
原理:idea控制台里的日志默认是从tomcat的localhost.log 和 catalina.log 两个文件中读出来的. https://blog.csdn.net/zhaijingkui/a ...
- 13、通过Docker-compose快速搭建Wordpress
一.compose定义 以下定义摘自docker官网:https://docs.docker.com/compose/overview/ Compose is a tool for defining ...
- 浅析java构造函数前的访问限定符问题
曾经一直有个问题困扰着我,我一直以为构造函数前面不能加任何东西,但偶然间看到了一本书上写的代码中,构造函数前加了public限定符,心里很是疑惑,构造函数前加毛访问限定符啊??! 在网上查了很多资料 ...
- Gulp:插件编写入门
之前挖了个坑,准备写篇gulp插件编写入门的科普文,之后迟迟没有动笔,因为不知道该肿么讲清楚Stream这货,毕竟,gulp插件的实现不像grunt插件的实现那么直观. 好吧,于是决定单刀直入了.文中 ...
- 微信小程序在当前页面设置其他页面的数据
如果其他页面用到的数据是 globalData, 那么直接在当前页面修改 globalData 数据即可. 如果其他页面用到的数据是 storage, 那么直接在当前页面修改 storage 数据即可 ...
- CentOS7 Rsync服务搭建-Rsync+Inotify架构实现实时同步
一.rsync 概念 1.rsyncrsync是类unix/linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsync主机同 ...
- Hive的一些理解
首先谈一下关于hive和hbase的区别的疑问(完全不是一个东西): 本质上来说hive和hbase没什么关系,虽然都是表,查数据等,但是他们根本就不是一个层面的东西 hive就是一个rapduce的 ...
- 关于GitHub上传没有记录(小绿块不显示的问题)
最近开始使用上github来上传保存自己在学习中所写过的代码,打算将自己每天的成果能有个保存,然后就利用上GitHub这么一个利器. 听说GitHub的那个绿块是用来记录每天的上传记录的,结果我将代码 ...