http://acm.hdu.edu.cn/showproblem.php?pid=5545
The Battle of GuanduTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 559 Accepted Submission(s): 249 Problem 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 M different battlefields whose numbers were 1 to M . There were also N villages nearby numbered from 1 to N . Cao Cao could train some warriors from those villages to strengthen his military. For village i , Cao Cao could only call for some number of warriors join the battlefield xi . However, Shao Yuan's power was extremely strong at that time. So in order to protect themselves, village i would also send equal number of warriors to battlefield yi and join the Yuan Shao's Army. If Cao Cao had called for one warrior from village i , he would have to pay ci 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. Input The first line of the input gives the number of test cases, T(1≤T≤30) . T test cases follow. Output For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y 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, y=−1 .
扫描二维码关注公众号,回复:
4981323 查看本文章
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 |
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
struct node {
int x;ll l;
node(){}
node(int x,ll l):x(x),l(l) {}
bool operator < (const node a) const { return l>a.l; }
};
int tot,u[N],v[N],p[N],pre[N];
int n,m,q[N],x[N],y[N],c[N],w[N];
void add(int a,int b,int c) {
v[tot]=b;p[tot]=c;pre[tot]=u[a];u[a]=tot++;
}
ll dis[N];
priority_queue<node>Q;
ll getmin() {
node now;
ll ret=0;memset(q,0,sizeof(q));
for (int i=1;i<=m;i++) dis[i]=MAX;
for (int i=1;i<=m;i++)
if (w[i]==0) dis[i]=0,Q.push(node(i,0));
while (!Q.empty()) {
now=Q.top();Q.pop();
if (q[now.x]) continue ;q[now.x]=1;
for (int i=u[now.x];i!=-1;i=pre[i])
if (dis[v[i]]>now.l+p[i]) {
dis[v[i]]=now.l+p[i];Q.push(node(v[i],dis[v[i]]));
}
}
for (int i=1;i<=m;i++)
if (w[i]==2&&dis[i]==MAX) return -1;
else if (w[i]==2) ret+=dis[i];
return ret;
}
int main()
{
int i,t,ca;
scanf("%d", &t);
for (ca=1;ca<=t;ca++) {
scanf("%d%d", &n, &m);
for (i=1;i<=n;i++) scanf("%d", &x[i]);//曹操
for (i=1;i<=n;i++) scanf("%d", &y[i]);//袁绍
for (i=1;i<=n;i++) scanf("%d", &c[i]);//消耗
for (i=1;i<=m;i++) scanf("%d", &w[i]);//战略方案
tot=0;memset(u,-1,sizeof(u));
for (i=1;i<=n;i++) add(y[i],x[i],c[i]);
printf("Case #%d: %I64d\n", ca, getmin());
}
return 0;
}