Silver Cow Party(最短路,好题)
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
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
#include <iostream>
#include <deque>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAXV = ;
const int inf = 0x3f3f3f3f;
int t[MAXV][MAXV], d1[MAXV], d2[MAXV];
int que[MAXV<<];
bool in[MAXV];
int n, m, x; void spfa(int * d)
{
memset(in, false, sizeof(in));
memset(d + , inf, sizeof(int) * n);//memset(d, inf, sizeof(d)) if wrong
d[x] = ;
int tail = -;
que[++tail] = x;
in[x] = true;
while(tail != -){
int cur = que[tail];
tail--;
in[cur] = false;
for(int i = ; i <= n; i++){
if(d[cur] + t[cur][i] < d[i]){
d[i] = d[cur] + t[cur][i];
if(in[i] == false){
que[++tail] = i;
in[i] = true;
}
}
}
}
} void tran()
{
int i, j;
for(i = ; i <= n; i++){
for(j = ; j <= i; j++){
swap(t[i][j], t[j][i]);
}
}
} int main()
{
while(scanf("%d %d %d", &n, &m, &x) != EOF){
memset(t, inf, sizeof(t));
while(m--){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
t[a][b] = c;
}
spfa(d1);
tran();
spfa(d2);
int ans = -;
for(int i = ; i <= n; i++){
if(d1[i] != inf && d2[i] != inf)
ans = max(ans, d1[i] + d2[i]);
}
printf("%d\n", ans);
}
return ;
}
Silver Cow Party(最短路,好题)的更多相关文章
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ3268 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算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- (poj)3268 Silver Cow Party 最短路
Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...
- poj 3268 Silver Cow Party(最短路dijkstra)
描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...
- TZOJ 1693 Silver Cow Party(最短路+思维)
描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big ...
- USACO 2011 February Silver Cow Line /// 康拓展开模板题 oj22713
题目大意: 输入n k,1-n的排列,k次操作 操作P:输入一个m 输出第m个排列 操作Q:输入一个排列 输出它是第几个排列 Sample Input 5 2P3Q1 2 5 3 4 Sample O ...
- B - B Silver Cow Party (最短路+转置)
有n个农场,编号1~N,农场里奶牛将去X号农场.这N个农场之间有M条单向路(注意),通过第i条路将需要花费Ti单位时间.选择最短时间的最优路径来回一趟,花费在去的路上和返回农场的这些最优路径的最长时间 ...
随机推荐
- 浅谈sql中的in与not in,exists与not exists的区别
转 浅谈sql中的in与not in,exists与not exists的区别 12月12日北京OSC源创会 —— 开源技术的年终盛典 » sql exists in 1.in和exists ...
- android:configChanges属性总结
http://blog.csdn.net/zhaokaiqiang1992/article/details/19921703 android中的组件Activity在manifest.xml文件中可以 ...
- 使用VSTS/TFS搭建iOS持续集成环境
TFS 自2015版开始支持跨平台的持续集成环境,通过提供开源的build agent为 Windows / linux / macOS 提供了统一的持续集成环境管理能力.这篇文章给大家介绍一下如何使 ...
- Oracle的FRA(Flash Recovery Area)的好处
如果FRA的空间耗尽,只会影响到这个Oracle实例自身.所以不会耗尽所有磁盘空间从而影响到其它的数据库实例或其它应用.
- C# 动态修改dll的签名 以及修改引用该dll文件的签名
在读取RedisSessionStateProvider配置 提到用mono ceil 来修改程序集以及它的签名,里面GetPublicKey 和GetPubliKeyToken 方法里面那个字符串的 ...
- VS.PHP详细破解教程,用Visual Studio编写PHP代码插件PhpTools
一.准备文件:(下载地址:http://download.csdn.net/detail/wulang1988/9662363) Default.aspx是解决在线破解文件:PhptoolCracke ...
- 转:VC解析XML文件-CMarkup的使用详解
本篇文章是对VC解析XML文件-CMarkup的使用进行了详细的分析介绍,需要的朋友参考下 VC解析XML文件的工具有很多,CMarkup, tinyXML,还有IBM的,MS的等等. 据说tinyX ...
- 【css】ie6 和 ie7 下 position 与 overflow 的问题
前几天做的项目中碰到这样一个问题,在 ie6 和 ie7 下,给父元素设置 overflow:hidden 不起作用无法隐藏,后来发现是子元素中有设置 position:relative,如果子元素删 ...
- [转载] Calculating Entropy
From: johndcook.com/blog For a set of positive probabilities pi summing to 1, their entropy is defi ...
- Pro ASP.NET MVC –第六章 MVC的基本工具
在本章,我们将介绍每个MVC程序员"武器库"的三个重要工具:依赖注入容器.单元测试框架和mock工具.在本书,对于三个工具分别都只用了一种方式实现,但每个工具都还有其他的实现方式. ...