ZOJ 3080 ChiBi [圖論]

watashi's mm is so pretty as well as smart. Recently, she has watched the movie Chibi. So she knows more about the War of ChiBi. In the war, Cao Cao had 800,000 soldiers, much more than his opponents'. But he was defeated. One of the mistakes he made was that he connected some of his boats together, and these boats were burned by the clever opponents.

Then an interesting problem occurs to watashi's mm. She wants to use this problem to check whether watashi is as smart as her. However, watashi has no idea about the problem. So he turns to you for help.

You know whether two boats are directly connected and the distance between them. And Fire's speed to spread between boats is 1m/s. You also know the time your soldiers need to travel from your camp to each boat. Because burning Cao Cao's boat is a very dangerous job, you must choose the least number of soldiers, and each one can only burn one boat. How much time do you need to burn all the Cao Cao's boats?

Input

The input contains several test cases. Each test case begins with a line contains only one integer 0 <= N <= 1000, which indicates the number of boats. The next N lines, each line contains N integers in range [0, 10000], the jth number in the ith line is the distance in metre between the ith boat and the jth boat, if the number is -1, then these two boats are not directly connected (d(i, j) == d(j, i) && d(i, i) == 0). Then N intergers in range [0, 10000], the ith number is the time in second your soldiers need to travel from the camp to the ith boat. What's more Cao Cao is not that stupid, so he won't connect more than 100 boats together.

Output

The shortest time you need to burn all the Cao Cao's boats counting from the soldiers leave the camp in a single line.

Sample input

4
0 1 2 -1
1 0 4 -1
2 4 0 -1
-1 -1 -1 0
1 2 4 8

Sample Output

8

分析:

派出士兵最少意味着每一個連通塊僅派出一名士兵。所以可以對每一個節點跑一次SPFA,算出每個連通塊所有船被燒着的最短時間,再求出最長的連同塊燒着時間。

code:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define MAXN 1000
#define INF 0x10101010
using namespace std;
int n,trcnt;
int map[MAXN+5][MAXN+5];
int dis[MAXN+5],unit[MAXN+5],mts[MAXN+5],minft[MAXN+5];
struct edge{
	int to,next,val;
}adj[MAXN*MAXN+5];
int head[MAXN+5],cnt;
int rtsearch(int x){return unit[x]==x?x:unit[x]=rtsearch(unit[x]);}
void add(int u,int v,int w)
{
	adj[++cnt].to=v;
	adj[cnt].val=w;
	adj[cnt].next=head[u];
	head[u]=cnt;
}
int SPFA(int x)
{
	queue<int>q;
	int maxt=0,i;
	bool inq[MAXN+5];
	memset(dis,INF,sizeof dis);
	memset(inq,false,sizeof inq);
	dis[x]=0;
	q.push(x);
	inq[x]=true;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		inq[u]=false;
		for(i=head[u];i;i=adj[i].next)
		{
			int v=adj[i].to;
			if(dis[u]+adj[i].val<dis[v])
			{
				dis[v]=dis[u]+adj[i].val;
				if(!inq[v])
				{
					inq[v]=true;
					q.push(v);
				}
			}
		}
	}
	for(i=1;i<=n;i++)
		if(dis[i]>maxt&&dis[i]!=INF)
			maxt=dis[i];
	return maxt;
}
int main()
{
	int i,j;
	memset(dis,INF,sizeof dis);
	while(~scanf("%d",&n))
	{
		cnt=0;
		memset(minft,INF,sizeof minft);
		memset(head,0,sizeof head);
		for(i=1;i<=n;i++)
			unit[i]=i;
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
			{
				scanf("%d",&map[i][j]);
				if(map[i][j]!=-1&&i!=j)
				{
					add(i,j,map[i][j]);
					unit[rtsearch(j)]=rtsearch(i);
				}
			}
		for(i=1;i<=n;i++)
			scanf("%d",&mts[i]);
		int maxt;
		for(i=1;i<=n;i++)
		{
			maxt=SPFA(i)+mts[i];
			if(maxt<minft[unit[i]])
				minft[unit[i]]=maxt;
		}
		maxt=0;
		for(i=1;i<=n;i++)
			if(maxt<minft[i]&&minft[i]!=INF)
				maxt=minft[i];
		printf("%d\n",maxt);
	}
}


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