hdu 5545 The Battle of Guandu 最短路径

The Battle of Guandu

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 394    Accepted Submission(s): 179


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.

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  wi . Some of the battlefields with  wi=2  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  wi=1  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  wi=0  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,  T(1T30) T  test cases follow.

Each test case begins with two integers  N  and  M(1N,M105)  in one line.

The second line contains  N  integers separated by blanks. The  ith  integer  xi(1xiM)  means Cao Cao could call for warriors from village  i  to battlefield  xi .

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

The next line contains  N  integers separated by blanks. The  ith  integer  ci(0ci105)  means the number of money Cao Cao had to pay for each warrior from this village.

The last line contains  M  integers separated by blanks. The  ith  number  wi(wi0,1,2)  means the importance level of  ith  battlefield.
 

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 .
 

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

题目大意:有n个村庄,m个战场。对于村庄i,曹操可以支付c[i]*num元,使得这个村庄派遣num个士兵去战场x[i]为曹操作战,同时,这个村庄也会派遣num个士兵去战场y[i]为袁绍作战。对于每个战场,有3种重要度(0,1,2)。重要度为2表示该战场曹操的士兵数必须大于袁绍的士兵数;重要度为1表示该战场曹操的士兵数必须不小于袁绍的士兵数;重要度为0则对两方士兵数无任何要求。求曹操若想赢得战争,至少要支付多少钱。


由贪心可知,在重要度为2的战场,曹操的士兵恰好比袁绍多一,在重要度为1的战场,两军士兵数相等,否则减去曹操士兵数可以使得花费更低。

对于一个村庄来说,花费w会向x战场增加曹兵,会向y战场增加袁兵,所以一个村庄其实就是一条边x->y,使得x的曹兵+1,是的y的曹兵-1,对于一个重要度为2的战场x,我们向这里增加一个曹兵,那么他对应的y战场曹兵会减一,如果y重要度为0,那么不用管了,如果为1或者2,那么还要在y中加曹兵,对应从y继续往其他点走,其实就是从x走到最近的重要度为0的点,对所有重要度为2的战场跑一遍最短路径,将最短的花费加起来就是答案,但是因为数据量很大,所以把边逆过来,这样只跑一次多源最短路径就好了。

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;

const int maxn = 1e5 + 10;
const long long INF = 1e10;
struct node 
{
    int u, v, w, next;
}E[2 * maxn];

int n, m;
int x[maxn], y[maxn], c[maxn], level[maxn];
int vis[maxn];
int head[maxn], cnt;
long long dist[maxn];

void addedge(int u, int v, int w)
{
    E[cnt].u = u;
    E[cnt].v = v;
    E[cnt].w = w;
    E[cnt].next = head[u];
    head[u] = cnt++;
}

void spfa()
{
    queue<int> Q;
    Q.push(0);
    vis[0] = 1;
    while (!Q.empty())
    {
        int u = Q.front(); Q.pop();
        vis[u] = 0;
        for (int i = head[u]; i != -1; i = E[i].next)
        {
            int v = E[i].v;
            int w = E[i].w;
            if (dist[v] > dist[u] + w)
            {
                dist[v] = dist[u] + w;
                if (vis[v] == 0)
                {
                    Q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
}

int main()
{
    int T;
    int cas = 0;
    scanf("%d", &T);
    while (T--)
    {
        cas ++;
        memset(head, -1, sizeof(head));
        memset(vis, 0, sizeof(vis));
        cnt = 0;
        scanf("%d%d", &n, &m);
        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("%d", &c[i]);
        for (int i = 1; i <= m; i++)
        {
            scanf("%d", &level[i]);
            dist[i] = INF;
        }
        
        for (int i = 1; i <= n; i++)
        {
            addedge(y[i], x[i], c[i]);
            if (level[y[i]] == 0)
                addedge(0, y[i], 0);
        }
        dist[0] = 0;
        spfa();
        //for (int i = 0; i <= m; i++) printf("|%d ", dist[i]);
        long long ans = 0;
        for (int i = 1; i <= m; i++)
        {
            if (level[i] == 2)
            {
                if (dist[i] == INF)
                {
                    ans = INF;
                    break;
                }
                else
                    ans += dist[i];
            }
        }
        if (ans == INF)
            printf("Case #%d: -1\n", cas);
        else
            printf("Case #%d: %lld\n", cas, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值