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单位时间.选择最短时间的最优路径来回一趟,花费在去的路上和返回农场的这些最优路径的最长时间 ...
随机推荐
- 深入了解javascript事件流
摘要:事件流这个东西是比较重要的,为了让自己更加理解js中的事件流,必须整理整理,梳理一下事件流的各种东西啊.本文大部分内容参考<javascript高级程序设计第三版> 先来一段书里的原 ...
- 大数据并行计算利器之MPI/OpenMP
大数据集群计算利器之MPI/OpenMP ---以连通域标记算法并行化为例 1 背景 图像连通域标记算法是从一幅栅格图像(通常为二值图像)中,将互相邻接(4邻接或8邻接)的具有非背景值的像素集合提取出 ...
- CentOS挂载NTFS移动硬盘
CentOS操作系统默认无法挂在NTFS格式的移动硬盘,解决方案之一为使用ntfs-3g挂在: 1. 在其官网上下载安装包: http://www.tuxera.com/community/open- ...
- Shell脚本学习入门(一)
1.Shell脚本是解释型的语言. 2.Shell脚本建立的基本步骤: 3.Shell脚本文件的第一行一般可以是:"#! 路径名 -(选项)", 为了要使Shell脚本有移植性,请 ...
- 题目一:一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?
题目一:一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)? //一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米 doub ...
- OpenGL学习进程(12)第九课:矩阵乘法实现3D变换
本节是OpenGL学习的第九个课时,下面将详细介绍OpenGL的多种3D变换和如何操作矩阵堆栈. (1)3D变换: OpenGL中绘制3D世界的空间变换包括:模型变换.视图变换.投影变换和视口 ...
- Windows系统补丁KB2962872导致InstallShield无法启动(解决方案已更新)
20140717最新更新: Flexera Software发布了临时补丁包,该补丁包暂时禁止了InstallShield Trialware功能(中国区用户很少有用此功能)两种安装方法: 方法1. ...
- 什么是automatic variable?
看代码符号$?搞不清楚是什么? 看代码. $share = Get-WmiObject -Class Win32_Share -ComputerName $Server.name -Credent ...
- Javascript基础恶补
1.字符集:Javascript采用Unicode字符集,支持地球上所有在用的语言. 2.区分大小写:Javascript区分大小写,HTML不区分大小写. 3.空格.换行.格式控制符:Javascr ...
- eclipse、myeclipse,svn插件subclipse 忘记密码的解决方法(win7、win8、xp)
如果是Windows7.Windows8系统只要删除当前用户目录下的AppData\Roaming\Subversion\auth\svn.simple 比如我的用户名taoweiji,就删除C:\U ...