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
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
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
Hint
题解:还是正向建图和逆向建图。就是求往返路程中最大的一条。
djistra:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int INF= 0x3fffffff; int map[2][1003][1003];
bool visited[1003];
int d[1003];
int ans[1003]; int max(int a,int b)
{
return a > b ? a : b;
} void prim(int n,int s,int flag)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = map[flag][s][i];
//cout<<d[i]<<endl;
}
visited[s] = true;
for(int i = 1;i < n;i++)
{
int min = INF;
int k;
for(int j = 1;j <= n;j++)
{
if(!visited[j] && min > d[j])
{
min = d[j];
k = j;
}
}
if(min == INF)
{
break;
}
visited[k] = true;
for(int j = 1;j <= n;j++)
{
if(!visited[j] && d[j] > d[k] + map[flag][k][j])
{
d[j] = d[k] + map[flag][k][j];
}
}
}
} int main()
{
int n,m,st;
while(scanf("%d%d%d",&n,&m,&st) != EOF)
{
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(i == j)
{
map[0][i][j] = 0;
map[1][i][j] = 0;
}
else
{
map[0][i][j] = INF;
map[1][i][j] = INF;
}
}
} int u,v,c;
for(int i = 0;i < m;i++)
{
scanf("%d%d%d",&u,&v,&c);
map[0][u][v] = c;
map[1][v][u] = c;
} prim(n,st,1);
for(int i = 1;i <= n;i++)
{
ans[i] = d[i];
}
prim(n,st,0);
int res = 0;
for(int i = 1;i <= n;i++)
{
ans[i] += d[i];
res = max(res,ans[i]);
} printf("%d\n",res);
} return 0;
}
Silver Cow Party的更多相关文章
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- Silver Cow Party(最短路,好题)
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- 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 (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 ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- TOJ1693(Silver Cow Party)
Silver Cow Party Time Limit(Common/Java):2000MS/20000MS Memory Limit:65536KByte Total Submit: ...
- POJ3268 Silver Cow Party(dijkstra+矩阵转置)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15156 Accepted: 6843 ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions:28457 Accepted: 12928 ...
随机推荐
- HDU 4696 Answers (2013多校10,1001题 )
Answers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- 分布式服务的事务如何处理?比如dubbo,服务与服务之间的事务怎么处理比较好,现在有没有开源的解决方案?
作者:何明璐链接:http://www.zhihu.com/question/29483490/answer/98237582来源:知乎著作权归作者所有,转载请联系作者获得授权. 首先是不建议采用XA ...
- WIN7提示“您已使用临时配置文件登陆”的解决方案
问题出现的很恶心.浪费时间不说,还是会让人弄的很烦躁. 首先,我可能是在个人的文件夹下,使用360强制删除了系统占用的文件, 具体是什么不知道了. 现在只想知道如何恢复,很反感,大半夜的了,弄了个这, ...
- 关于hessian接口类方法顺序及对象序列化的实战研究
前段时间,提供出去的hessian接口被调用时,调用方出现序列化异常以及其他莫名的异常.同事说hessian接口使用有2个注意事项: 1.对于已经存在的hessian接口,后续增加的方法只能加在文件末 ...
- 获取easyui calendar 属性
<div id="cc" class="easyui-calendar" style="width:250px;height:250px;&qu ...
- UBUNTU 14.04 INSTALL nsenter
cd /tmp; curl https://www.kernel.org/pub/linux/utils/util-linux/v2.25/util-linux-2.25.tar.gz | tar - ...
- 第三章 线程安全的DateFormat工具类
1.使用threadLocal包装DateFormat(太复杂,不推荐) 2.使用org.apache.commons.lang3.time.DateFormatUtils下的方法(推荐) DateF ...
- 《Pro JavaScript Techniques》中的一些函数
//获取元素的样式值. function getStyle(elem, name) { if (elem.style[name]) { return elem.style[name]; } else ...
- vue中的css作用域、vue中的scoped坑点
一.css作用域 之前一直很困扰css的作用域问题,即使是模块化编程下,在对应的模块的js中import css进来,这个css仍然是全局的.导致在css中需要加上对应模块的html的id/class ...
- HTML-Html开发之Viewport的使用
近年来随着移动端的快速发展,越来越多传统的web应用需要适配移动终端,下面记录一下如何通过viewport实现简单的不同型号的手机端的适配问题.不过在此之前,介绍一下如何通过Chrome浏览器,调试在 ...