HDU 5545 The Battle of Guandu(最短路)

本文探讨了一个基于图论的问题,曹操如何在不同战场间调动士兵以最小化成本,并确保在关键战场上占据优势。通过构建图模型并运用最短路径算法找到最优解。

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

思路:首先每次曹操从一个村庄调用一个兵到战场xi时,其实相当于同时往yi战场减少一个兵,如果yi战场的价值度是0的话那么是无所谓的,如果价值是2的话,那么这减少的一个兵你就要通过其他村庄转移来填补这一个减少的兵,否则就会输掉,那么这样一直转移到一个无用的战场,那么就可以满足条件了,所以按照这样建图然后跑一次最短路那么就是最少费用了(一开始以为是网络流什么的...大概数据范围也透露了一点...)


#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 100000+500;
int n,m;
#define INF 1LL<<38
int x[maxn],y[maxn],p[maxn],inq[maxn];
LL c[maxn],d[maxn];
vector<pair<int,LL> >e[maxn];
int main()
{
    int T,cas=1;
	scanf("%d",&T);
	while(T--)
	{
		memset(d,0,sizeof(d));
        scanf("%d%d",&n,&m);
		for(int i = 0;i<=n;i++)
			e[i].clear();
		for(int i = 1;i<=n;i++)
			scanf("%d",&x[i]);
		for(int i = 1;i<=n;i++)
			scanf("%d",&y[i]);
        for(int i = 1;i<=n;i++)
			scanf("%lld",&c[i]);
		for(int i = 1;i<=m;i++)
			scanf("%d",&p[i]);
		for(int i = 1;i<=n;i++)
		{
            if(p[x[i]]==0)continue;
			e[y[i]].push_back(make_pair(x[i],c[i]));
		}
		queue<int>q;
		for(int i = 1;i<=m;i++)
		{
			if(p[i]==0)
			{
				q.push(i);
				inq[i]=1;
                d[i]=0;
			}
			else
			{
				d[i]=INF;
				inq[i]=0;
			}
		}
		while(!q.empty())
		{
			int u = q.front();
			q.pop();
			inq[u]=0;
			for(int i = 0;i<e[u].size();i++)
			{
				int v = e[u][i].first;
				if(d[v]>d[u]+e[u][i].second)
				{
					d[v]=d[u]+e[u][i].second;
					if(inq[v])
						continue;
					inq[v]=1;
					q.push(v);
				}
			}
		}
		LL ans = 0;
		int flag = 0;
		for(int i = 1;i<=m;i++)
		{
			if(p[i]==2)
			{
				if(d[i]==INF)
				{
					flag = 1;
					break;
				}
				ans+=d[i];
			}
		}
		printf("Case #%d: ",cas++);
		if(flag)
			printf("-1\n");
		else
			printf("%lld\n",ans);
	}
}


Description

In the year of 200, two generals whose names are Cao Cao and Shao Yuan are fighting in Guandu. The battle of Guandu was a great battle and the two armies were fighting at  different battlefields whose numbers were 1 to . There were also  villages nearby numbered from 1 to . Cao Cao could train some warriors from those villages to strengthen his military. For village , Cao Cao could only call for some number of warriors join the battlefield . However, Shao Yuan's power was extremely strong at that time. So in order to protect themselves, village  would also send equal number of warriors to battlefield  and join the Yuan Shao's Army. If Cao Cao had called for one warrior from village , he would have to pay  units of money for the village. There was no need for Cao Cao to pay for the warriors who would join Shao Yuan's army. At the beginning, there were no warriors of both sides in every battlefield. 

As one of greatest strategist at that time, Cao Cao was considering how to beat Shao Yuan. As we can image, the battlefields would have different level of importance . Some of the battlefields with  were very important, so Cao Cao had to guarantee that in these battlefields, the number of his warriors was greater than Shao Yuan's. And some of the battlefields with  were not as important as before, so Cao Cao had to make sure that the number of his warriors was greater or equal to Shao Yuan's. The other battlefields with  had no importance, so there were no restriction about the number of warriors in those battlefields. Now, given such conditions, could you help Cao Cao find the least number of money he had to pay to win the battlefield?
 

Input

The first line of the input gives the number of test cases,  test cases follow. 

Each test case begins with two integers  and  in one line. 

The second line contains  integers separated by blanks. The  integer  means Cao Cao could call for warriors from village  to battlefield 

The third line also contains  integers separated by blanks. The  integer  means if Cao Cao called some number of warriors from village , there would be the same number of warriors join Shao Yuan's army and fight in battlefield 

The next line contains  integers separated by blanks. The  integer  means the number of money Cao Cao had to pay for each warrior from this village. 

The last line contains  integers separated by blanks. The  number  means the importance level of  battlefield.
 

Output

For each test case, output one line containing Case #x: y, where  is the test case number (starting from 1) and  is the least amount of money that Cao Cao had to pay for all the warriors to win the battle. If he couldn't win, .
 

Sample Input

2 2 3 2 3 1 1 1 1 0 1 2 1 1 1 1 1 2
 

Sample Output

Case #1: 1 Case #2: -1
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值