ZOJ 3080 ChiBi [图论]

本文探讨了一种在有限兵力条件下,高效烧毁敌方连通船只的策略问题。通过算法设计,实现对船只间的连接关系及距离进行分析,确定最少士兵数量及最短行动时间,确保所有敌船被烧毁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值