bzoj1774 USACO 2009 Dec Gold 2.Cow Toll Paths 過路費 翻譯+題解

Description

Like everyone else, FJ is always thinking up ways to increase his
revenue. To this end, he has set up a series of tolls that the cows
will pay when they traverse the cowpaths throughout the farm.

The cows move from any of the N (1 <= N <= 250) pastures conveniently
numbered 1..N to any other pasture over a set of M (1 <= M <= 10,000)
bidirectional cowpaths that connect pairs of different pastures A_j
and B_j (1 <= A_j <= N; 1 <= B_j <= N). FJ has assigned a toll L_j
(1 <= L_j <= 100,000) to the path connecting pastures A_j and B_j.

While there may be multiple cowpaths connecting the same pair of
pastures, a cowpath will never connect a pasture to itself. Best
of all, a cow can always move from any one pasture to any other
pasture by following some sequence of cowpaths.

In an act that can only be described as greedy, FJ has also assigned a
toll C_i (1 <= C_i <= 100,000) to every pasture. The cost of moving
from one pasture to some different pasture is the sum of the tolls
for each of the cowpaths that were traversed plus a *single additional
toll* that is the maximum of all the pasture tolls encountered along
the way, including the initial and destination pastures.

The patient cows wish to investigate their options. They want you
to write a program that accepts K (1 <= K <= 10,000) queries and
outputs the minimum cost of trip specified by each query. Query i
is a pair of numbers s_i and t_i (1 <= s_i <= N; 1 <= t_i <= N; s_i !=
t_i) specifying a starting and ending pasture.

Consider this example diagram with five pastures:

The 'edge toll' for the path from pasture 1 to pasture 2 is 3.
Pasture 2's 'node toll' is 5.

To travel from pasture 1 to pasture 4, traverse pastures 1 to 3 to
5 to 4. This incurs an edge toll of 2+1+1=4 and a node toll of 4
(since pasture 5's toll is greatest), for a total cost of 4+4=8.

The best way to travel from pasture 2 to pasture 3 is to traverse
pastures 2 to 5 to 3. This incurs an edge toll of 3+1=4 and a node
toll of 5, for a total cost of 4+5=9.

Input

* Line 1: Three space separated integers: N, M, and K

* Lines 2..N+1: Line i+1 contains a single integer: C_i

* Lines N+2..N+M+1: Line j+N+1 contains three space separated
        integers: A_j, B_j, and L_j

* Lines N+M+2..N+M+K+1: Line i+N+M+1 specifies query i using two
        space-separated integers: s_i and  t_i

Output

* Lines 1..K: Line i contains a single integer which is the lowest
        cost of any route from s_i to t_i

Sample Input

5 7 2

2

5

3

3

4

1 2 3

1 3 2

2 5 3

5 3 1

5 4 1

2 4 3

3 4 4

1 4

2 3

Sample Output

8

9

 

以下是翻譯:

Description

  跟所有人一樣,農夫約翰以着寧教我負天下牛,休叫天下牛負我的偉大精神,日日夜夜苦思生財之道。爲了發財,他設置了一系列的規章制度,使得任何一隻奶牛在農場中的道路行走,都要向農夫約翰上交過路費。

  農場中由N(1 <= N <= 250)片草地(標號爲1到N),並且有M(1 <= M <= 10000)條雙向道路連接草地A_j和B_j(1 <= A_j <= N; 1 <= B_j <= N)。奶牛們從任意一片草地出發可以抵達任意一片的草地。FJ已經在連接A_j和B_j的雙向道路上設置一個過路費L_j(1 <= L_j <= 100,000)。

  可能有多條道路連接相同的兩片草地,但是不存在一條道路連接一片草地和這片草地本身。最值得慶幸的是,奶牛從任意一篇草地出發,經過一系列的路徑,總是可以抵達其它的任意一片草地。

  除了貪得無厭,叫獸都不知道該說什麼好。FJ竟然在每片草地上面也設置了一個過路費C_i(1 <= C_i <= 100000)。從一片草地到另外一片草地的費用,是經過的所有道路的過路費之和,加上經過的所有的草地(包括起點和終點)的過路費的最大值。 任勞任怨的牛們希望去調查一下她們應該選擇那一條路徑。她們要你寫一個程序,接受K(1<= K <= 10,000)個問題並且輸出每個詢問對應的最小花費。第i個問題包含兩個數字s_i和t_i(1 <= s_i <= N; 1 <= t_i <= N; s_i != t_i),表示起點和終點的草地。

  考慮下面這個包含5片草地的樣例圖像:

  

  從草地1到草地3的道路的“邊過路費”爲3,草地2的“點過路費”爲5。

  要從草地1走到草地4,可以從草地1走到草地3再走到草地5最後抵達草地4。如果這麼走的話,需要的“邊過路費”爲2+1+1=4,需要的點過路費爲4(草地5的點過路費最大),所以總的花費爲4+4=8。

  而從草地2到草地3的最佳路徑是從草地2出發,抵達草地5,最後到達草地3。這麼走的話,邊過路費爲3+1=4,點過路費爲5,總花費爲4+5=9。

Input

  * 第1行: 三個空格隔開的整數: N, M和K

  * 第2到第N+1行: 第i+1行包含一個單獨的整數: C_i

  * 第N+2到第N+M+1行: 第j+N+1行包含3個由空格隔開的整數: A_j, B_j和L_j

  * 第N+M+2倒第N+M+K+1行: 第i+N+M+1行表示第i個問題,包含兩個由空格隔開的整數s_i和t_i

Output

  * 第1到第K行: 第i行包含一個單獨的整數,表示從s_i到t_i的最小花費。

Sample Input

5 7 2

2

5

3

3

4

1 2 3

1 3 2

2 5 3

5 3 1

5 4 1

2 4 3

3 4 4

1 4

2 3

Sample Output

8

9

以下爲題解:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int f[251][251],dis[251][251],g[251],s[251];
inline bool cmp(int x,int y)
{
	return g[x]<g[y];	
}
int main()
{
	int i,a,b,c,j,k,n,m,p,q,t;
	scanf("%d%d%d",&n,&m,&p);
	memset(f,1,sizeof(f));
	memset(dis,1,sizeof(dis));
	for(i=1;i<=n;i++) 
	{
		s[i]=i;
		scanf("%d",&g[i]);
		f[i][i]=g[i];		
	}	
	sort(s+1,s+n+1,cmp);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		dis[a][b]=dis[b][a]=min(dis[a][b],c);
	}	
	for(k=1;k<=n;k++)
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
			{
				dis[s[i]][s[j]]=min(dis[s[i]][s[j]],dis[s[i]][s[k]]+dis[s[k]][s[j]]);	
				f[s[i]][s[j]]=min(f[s[i]][s[j]],dis[s[i]][s[j]]+max(g[s[i]],max(g[s[k]],g[s[j]])));							
			}
	for(i=1;i<=p;i++)
	{
		scanf("%d%d",&q,&t);
		if(f[q][t]>=16843010)
			printf("-1\n");
		else
			printf("%d\n",f[q][t]);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章