TOJ1693(Silver Cow Party)
Silver Cow Party

Total Submit: 58 Accepted: 28
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 farmBi, 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
Hint
Source
思路:对原图求一次最短路,再对反图进行一次最短路,然后找最求解即可.
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <vector>
- #include <algorithm>
- #include <string>
- #include <queue>
- using namespace std;
- const int maxn = 1100;
- const int maxm = 101000;
- const int INF = 0x7fffffff;
- vector<pair<int, int> > g[maxn];
- vector<pair<int, int> > rg[maxn];//reverse;
- int gn, gm, x;
- bool inque[maxn];
- queue<int> Q;
- int d[maxn];//保存原图最短径.
- int rd[maxn];//保存反图的最短路径值.
- void spfa2(int s) {
- int i;
- memset(inque, false, sizeof(inque));
- for(i = 1; i < maxn; i++) rd[i] = INF;
- rd[s] = 0;
- while(!Q.empty()) Q.pop();
- Q.push(s);
- inque[s] = true;
- while(!Q.empty()) {
- int u = Q.front();
- Q.pop();
- for(i = 0; i < (int)rg[u].size(); i++) {
- int t = rg[u][i].first;
- if( rd[u] != INF && rd[u] + rg[u][i].second < rd[t]) {
- rd[t] = rd[u] + rg[u][i].second;
- if(!inque[t]) {
- inque[t] = true;
- Q.push(t);
- }
- }
- }
- inque[u] = false;
- }
- }
- void work(const int s) {
- int i;
- int maxv = -1;
- for(i = 1; i <= gn; i++) {
- if(i != s) {
- if(d[i] + rd[i] > maxv) {
- maxv = d[i] + rd[i];
- }
- }
- }
- printf("%d\n", maxv);
- }
- void spfa1(int s) {
- int i;
- memset(inque, false, sizeof(inque));
- for(i = 1; i < maxn; i++) d[i] = INF;
- d[s] = 0;
- while(!Q.empty()) Q.pop();
- Q.push(s);
- inque[s] = true;
- while(!Q.empty()) {
- int u = Q.front();
- Q.pop();
- for(i = 0; i < (int)g[u].size(); i++) {
- int t = g[u][i].first;
- if(d[u] != INF && d[u] + g[u][i].second < d[t]) {
- d[t] = d[u] + g[u][i].second;
- if(!inque[t]) {
- inque[t] = true;
- Q.push(t);
- }
- }
- }
- inque[u] = false;
- }
- }
- int main()
- {
- int i;
- int x, y, w;
- int start;
- pair<int, int> t;
- while(scanf("%d%d%d", &gn, &gm, &start) != EOF) {
- for(i = 1; i <= gn; i++) {
- g[i].clear();
- rg[i].clear();
- }
- for(i = 0; i < gm; i++) {
- scanf("%d%d%d", &x, &y, &w);
- t.first = y;
- t.second = w;
- g[x].push_back(t);
- t.first = x;
- t.second = w;
- rg[y].push_back(t);
- }
- spfa1(start);
- spfa2(start);
- work(start);
- }
- return 0;
- }
TOJ1693(Silver Cow Party)的更多相关文章
- POJ3268(Silver Cow Party)
题意: 有n头牛去第x个点开party(有点高大上~),单向路,去到还得回来,问这n头牛每一头花费的总时间的最大值是多少 模板spfa: #include <iostream> #incl ...
- 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(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- 【POJ - 3268 】Silver Cow Party (最短路 Dijkstra算法)
Silver Cow Party Descriptions 给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x, ...
- 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 (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions:28457 Accepted: 12928 ...
- Silver Cow Party---poj3268(最短路,迪杰斯特拉)
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u De ...
随机推荐
- php和js根据子网掩码和ip计算子网
php $ip = '192.168.6.1'; $mask = '255.255.2.0'; $sub_net = array();//子网 $ip_explode = explode('.', $ ...
- 如何更有效学习php开源项目的源码
一.先把源代码安装起来,结合它的文档和手册,熟悉其功能和它的应用方式. 二.浏览源代码的目录结构,了解各个目录的功能. 三.经过以上两步后相信你对这个开源的产品有了一个初步的了解了,那现在就开始分析它 ...
- 练习2 H题 - 求数列的和
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 数列的 ...
- 在Docker下部署Nginx
在Docker下部署Nginx 在Docker下部署Nginx,包括: 部署一个最简单的Nginx,可以通过80端口访问默认的网站 设置记录访问和错误日志的路径 设置静态网站的路径 通过proxy_p ...
- JAVA面试题相关基础知识
1.面向对象的特征有哪些方面 ①抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节 ...
- c++和java(c#)之间的pk
个人认为本文较偏激,且年代较久远,但可以一看. 转自c++和java(c#)之间的pk 1.谁好谁坏? 如同当初我没有想到会进入java阵营一样,这次闯入c++阵营也是意料之外的.多年前,受到微软的影 ...
- Bitmap介绍
转自:http://blog.csdn.net/xgdofull/article/details/5424611 简单的说就是用数组存放若有数据就标志为1或true,若不存在标志为0或false.比如 ...
- Eclipse配置Git
一.Eclipse上安装GIT插件EGit Eclipse的版本eclipse-java-helios-SR2-win32.zip(在Eclipse3.3版本找不到对应的 EGit插件,无法安装) E ...
- lc面试准备:Power of Two
1 题目 Given an integer, write a function to determine if it is a power of two. 接口 boolean isPowerOfTw ...
- zoj 3088 Easter Holidays(最长路+最短路+打印路径)
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...