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单位时间.选择最短时间的最优路径来回一趟,花费在去的路上和返回农场的这些最优路径的最长时间 ...
随机推荐
- paip.mysql 批量kill 连接.
paip.mysql 批量kill 连接. 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net ...
- 使用iMovie和Keynote制作App Preview
App Preview是什么 App Preview就是一段15-30秒的短视频,用来展示你的应用的特性.用户界面.交互方式等内容.在App Store你的应用的详细信息页面里,放在原来的截图之前.体 ...
- Leetcode 38 Count and Say 传说中的递推
class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...
- Leetcode 160 Intersection of Two Linked Lists 单向链表
找出链表的交点, 如图所示的c1, 如果没有相交返回null. A: a1 → a2 ↘ ...
- aehyok.com的成长之路三——框架结构
前言 首先奉上个人网站地址传送门:aehyok.com. aehyok.com的成长之路一——开篇 中主要阐述了自己为什么建立自己的网站,以及个人网站的大致方向. aehyok.com的成长之路二—— ...
- Number Range 管理之并行缓冲
Number Range 管理之并行缓冲: 常用的事务代码SNRO,SM56还有一些业务专用的号码管理,可以在SPRO中查找: SNRO :Number Range 管理 一般的操作是维护号码范围.如 ...
- 【转】Xcode概览(Xcode 6版):循序渐进认识Xcode
该系列文章翻译自苹果的Xcode Overview文档,对大部分开发者来说,已经非常熟悉Xcode的功能和特性,不过伴随着iOS 8 SDK的发布,Xcode 6中也有些许调整,所以对该文档进行了翻译 ...
- latextools \cite 自动补全
最近在用latex写毕业论文,编辑环境用的是Sublime Text 2 加 latextools 插件,在使用latextools的\cite命令来引用参考文献时,我们希望输入\cite{ 后自动弹 ...
- Android系列--DOM、SAX、Pull解析XML
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...
- 文档大师 搜狗拼音无法输入汉字_乱码的解决方法_VB6程序
文档大师用 搜狗拼音无法输入汉字,显示的内容和输入的内容不一致.解决方法: 把中文输入里面的那个“美式键盘”再删掉就好用了,只保留搜狗输入法即可!