码迷,mamicode.com
首页 > 其他好文 > 详细

Luogu P1821 [USACO07FEB]银牛派对Silver Cow Party

时间:2018-04-19 19:34:53      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:cow   pat   uri   输入输出格式   fine   ret   一个   direction   ber   

P1821 [USACO07FEB]银牛派对Silver Cow Party

题目描述

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?

寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100)。

每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。

输入输出格式

输入格式:

 

第一行三个整数N,M, X;

第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti。

 

输出格式:

 

一个整数,表示最长的最短路得长度。

 

输入输出样例

输入样例#1:
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

  

输出样例#1:
10

  

 和我的另一篇题解大概一个思路,不再解释,附上链接

http://www.cnblogs.com/bljfy/p/8835733.html

代码

 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define MAXN 100003
#define INF 123456789

using namespace std;

int n, m, x, first[1008], next[MAXN], u[MAXN], v[MAXN], w[MAXN], dis[1008], ans[1008], Ans;

queue<int> P; bool vis[1008];

void Shortest_Path_Faster_Algorithm(int s) {
	while(!P.empty()) {
		int t = P.front(); P.pop(); int k = first[t];
		while(k != -1) {
			if(dis[v[k]] > dis[u[k]]+w[k]) {
				dis[v[k]] = dis[u[k]]+w[k];
				if(vis[v[k]] == 0) {P.push(v[k]); vis[v[k]] = 1;}
			}
			k = next[k];
		}
		vis[t] = 0;
	}
}

int main() {
	scanf("%d%d%d", &n, &m, &x);
	memset(first, -1, sizeof(first));
	for(int i=1; i<=n; i++) {dis[i] = INF;}
	dis[x] = 0; P.push(x);
	for(int i=1; i<=m; i++) {scanf("%d%d%d", &u[i], &v[i], &w[i]); next[i] = first[u[i]]; first[u[i]] = i;}
	vis[x] = 1;
	Shortest_Path_Faster_Algorithm(x);
	for(int i=1; i<=n; i++) {if(dis[i] != INF) ans[i] += dis[i];}
	memset(vis, false, sizeof(vis));
	memset(first, -1, sizeof(first));
	memset(next, 0, sizeof(next));
	for(int i=1; i<=n; i++) {dis[i] = INF;}
	dis[x] = 0;
	while(!P.empty()) {P.pop();}
	P.push(x);
	for(int i=1; i<=m; i++) {next[i] = first[v[i]]; first[v[i]] = i;}
	vis[x] = 1;
	swap(u, v); Shortest_Path_Faster_Algorithm(x);
	for(int i=1; i<=n; i++) {if(dis[i] != INF) ans[i] += dis[i]; Ans = max(Ans, ans[i]);}
	printf("%d", Ans); return 0;
}

 

  

 

 

Luogu P1821 [USACO07FEB]银牛派对Silver Cow Party

标签:cow   pat   uri   输入输出格式   fine   ret   一个   direction   ber   

原文地址:https://www.cnblogs.com/bljfy/p/8885510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!