1. 程式人生 > >poj 3268 Silver Cow Party 【最短路,有向圖】

poj 3268 Silver Cow Party 【最短路,有向圖】

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18401 Accepted: 8421

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: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai
 to farm Bi, 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

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units. dijkstral演算法,同時求去的回來的最短路,相加後的最大值就是所求 程式碼:
#include<cstdio>
#include<cstring> 
#include<cmath>
#define INF 0x3f3f3f
#define MAXN 1000+10
int min(int x,int y)
{
	return x<y?x:y;
}
int max(int x,int y)
{
	return x>y?x:y;
} 
int n,m,p;
int a,b,c;
int cost[MAXN][MAXN];
int d[MAXN],vis[MAXN];//去的最短距離 
int d1[MAXN],vis1[MAXN];//回來的最短距離 
void djs(int s)
{
	memset(vis,0,sizeof(vis));
	memset(vis1,0,sizeof(vis1));
	memset(d,INF,sizeof(d));
	memset(d1,INF,sizeof(d1));
	d[s]=0;
	d1[s]=0;
	for(int i=1;i<=n;i++)//n個節點 
	{
		int v=-1;
		int v1=-1;
		//相鄰最小距離的頂點 
		for(int u=1;u<=n;u++)
		{
			if(!vis[u]&&(v==-1||d[u]<d[v]))
				v=u;
			if(!vis1[u]&&(v1==-1||d1[u]<d1[v1]))
				v1=u;
		}
		vis[v]=1;
		vis1[v1]=1;
		for(int u=1;u<=n;u++)
		{
			d[u]=min(d[u],d[v]+cost[v][u]);
			d1[u]=min(d1[u],d1[v1]+cost[u][v1]);
		 } 		 
	}
}
 int main()
 {
 	while(~scanf("%d%d%d",&n,&m,&p))
 	{
	 	memset(cost,INF,sizeof(cost));
	 	for(int i=1;i<=m;i++)
	 	{
	 		scanf("%d%d%d",&a,&b,&c);
	 		if(cost[a][b]>c)
	 			cost[a][b]=c;
		 }
		 djs(p);
		 int maxn=0;
		 for(int i=1;i<=n;i++)
		 {
		 	if(i==p)
		 		continue;
		 	maxn=max(maxn,d[i]+d1[i]);
		 }
		 printf("%d\n",maxn);
	 }
 	return 0;
 }


相關推薦

poj 3268 Silver Cow Party 短路

Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18401 Accepted: 8421 Description One cow from each of N

POJ 3268 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

POJ--3268 Silver Cow Party短路

iostream {} push prior namespace sca author sil main 題目電波:POJ--3268 Silver Cow Party 跑兩遍 dijkstra 就好了 弱智題 #include<iostream&

poj3268 Silver Cow Party短路

題目連結:http://poj.org/problem?id=3268 題意:有n個農場,在x號農場有一個派對,農場之間有m條單向邊連線著這些農場,每條邊有一定的時間花費,每隻牛都會從自己農場出發去參

POJ-3268 -D - 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 tota

Poj 3268 Silver Cow Party (短路)

題意:一張N*N的有向圖,M條邊,每個點上都有一頭牛。給出一個X,表示牛的目的地。求出所有牛到達X的最短路。然後再求出所有牛回到自己原來位置的最短路。然後計算哪頭牛使用時間最長。 題解:兩遍dijkstra,搞定。詳情看程式碼註釋。 #include<cstdio&g

poj 3268 Silver Cow Party 短路/dijkstra

#include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <algorithm> #include <i

POJ 3268 Silver Cow Party(雙向短路

Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow p

POJ 3268 Silver Cow Party

turn border can rate hat n) output conn stream Language: Default Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K To

POJ-3268-Silver Cow Party(迪傑斯特拉 多點到star和star到多點)

D - Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice P

poj 3268 Silver Cow Party 題解

大意:有n個牧場,編號1到n,每個牧場有一頭奶牛。現在所有奶牛要到編號為x的牧場聚會,路徑是單向的,奶牛都很聰明,只走最短路徑,問哪頭奶牛來回走的路徑之和最大,輸出這個最大值。 思路:建立兩個鄰接表(一個出邊表,一個入邊表),然後分別對兩個鄰接表使用一次SPFA,得到的路

Silver Cow Party短路 + Dijkstra + 鄰接表 + 優先佇列)

Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11348 Accepted: 5077 Description One cow from each of N farms (1 ≤ N 

Silver Cow Party短路 很有意思的題)

Line 1: One integer: the maximum of time any one cow must walk. 求兩次最短路,第一次求x到其餘各點的最短路,第二次求各點到x的最短路。前者易於解決,直接應用spfa或其他最短路演算法即可,後者要先將鄰接矩陣轉置再執行最短路演算法。 為什麼進

Silver Cow Party 雙向短路

Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going

POJ 2112短路+二分+網絡流

space ret print dinic == con map ini set 題目描述:(轉)k個機器,每個機器最多服務m頭牛。c頭牛,每個牛需要1臺機器來服務。告訴你牛與機器每個之間的直接距離。問:讓所有的牛都被服務的情況下,使走的最遠的牛的距離最短,求這個距離。 #

LightOJ 1019-Brush (V)短路模板題

Tanvir returned home from the contest and got angry after seeing his room dusty. Who likes to see a dusty room after a brain storming programming contest?

5521  Meeting (短路 技巧建)

Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his  fences they were separated into differen

JoiOI 走廊潑水節小生成樹還原完全

貪心每次取當前邊權值+1還原,每次還原size[x]∗size[y]−1size[x]*size[y]-1size[x]∗size[y]−1條邊。 size[i]size[i]size[i]表示點iii所處的並查集集合中的元素個數: #include <c

小樹形圖--小生成樹 poj 3164 Command Network

【最小樹形圖】: 就是給有向帶權圖中指定一個特殊的點root,求一棵以root為根的有向生成樹T,並且T中所有邊的總權值最小。 最小樹形圖必須有一個根,而且選擇不同的點作為根,也是不一樣的結果。 最小樹形圖必須包含圖中的每一個節點,並且均可通過有向邊到達根節點root 最

第六章 短路徑——(Floyd-Warshall、Dijkstra、Bellman-Ford)

數組 opened 表示 printf 開始 style logs include 五行 一、Floyd-Warshall——加入點(多源最短路徑,核心算法只有五行) 城市之間的最短路徑 輸入: 4 8 1 2 2 1 3 6 1 4 4 2 3 3 3 1 7 3 4