2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

The Battle of Guandu

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

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

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.

Output

For each test case, output
one line containing Case #x:, where x is the test case number (starting
from 1). Then output 4 lines with 4 characters each. indicate the
recovered board.

Sample Input

22 32 31 11 10 1 21 11112

Sample Output

Case #1: 1Case #2: -1

HINT

题意

在一场战争中,有m(<=100000)个战场和n(<=100000)个村 庄,每个战场有一个重要度,重要度为0表示在这个战场己方输赢无所谓,重要度为1表示己方不能输,重要度为2表示己方必须胜出,己方获得战争的最终胜利当 且仅当己方在每个战场的战果均不违背其重要度,每个战场输赢的判据只有人数,人多的一方胜出,若两方人数相同则打平,对于第i个村庄,每花费c[i]的代 价能够征用一个人派到己方x[i]战场,同时有一个人会跑到敌方y[i]战场,问己方能否获得战争的最终胜利,若能,求出最小代价。

题解:

建图跑spfa就好,如果你考虑每个战场我方人数-敌方人数的数量的话,整个战场的人数实际上是守恒的。你可以花费c[i]代价,使得x[i]加一个人,y[i]减一个人。那么我们就建边跑最短路就好啦,让不重要的战场当成源点,重要的战场当成你要求的最短路就好了

d[i]表示从不重要战场拉一个人到i战场需要的最小代价,只要拉一个人就会胜利

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<vector>
using namespace std;
#define maxn 100005
long long inf = 999999999999999LL;
int x[maxn];
int y[maxn];
long long c[maxn];
int p[maxn];
int vis[maxn];
long long d[maxn];
vector<pair<int,long long> >G[maxn];
int main()
{
    int t;scanf("%d",&t);
    for(int cas = 1;cas <= t;cas++)
    {
        int n,m;scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
            G[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;
            G[y[i]].push_back(make_pair(x[i],c[i]));
        }

        queue<int> Q;
        for(int i=1;i<=m;i++)
        {
            if(p[i]==0)
            {
                d[i]=0;
                vis[i]=1;
                Q.push(i);
            }
            else
            {
                d[i]=inf;
                vis[i]=0;
            }
        }

        while(!Q.empty())
        {
            int now = Q.front();
            Q.pop();
            vis[now]=0;
            for(int i=0;i<G[now].size();i++)
            {
                int v = G[now][i].first;
                if(d[v]>G[now][i].second + d[now])
                {
                    d[v] = G[now][i].second + d[now];
                    if(vis[v])continue;
                    Q.push(v);
                    vis[v]=1;
                }
            }
        }

        long long 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];
            }
        }
        if(flag)
            printf("Case #%d: -1\n",cas);
        else
            printf("Case #%d: %lld\n",cas,ans);
    }
}
时间: 2024-10-05 15:38:38

2015南阳CCPC F - The Battle of Guandu 多源多汇最短路的相关文章

2015南阳CCPC C - The Battle of Chibi DP

C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao

2015南阳CCPC L - Huatuo&#39;s Medicine 水题

L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous doctor. He use identical bottles to carry the medicine. There are different types of medicine. Huatuo put medicines into the bottles and chain these b

2015南阳CCPC D - Pick The Sticks 背包DP.

D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting

2015南阳CCPC H - Sudoku 数独

H - Sudoku Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. Actually, Yi Sima was playing it different. First of all, he tried to generate a 4×4 board

2015南阳CCPC A - Secrete Master Plan 水题

D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Master Mind KongMing gave Fei Zhang a secrete master plan stashed in a pocket. The plan instructs how to deploy soldiers on the four corners of the city wall. Unfortunately, w

2015南阳CCPC H - Sudoku 暴力

H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. Actually, Yi Sima was playing it different. F

2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom period, there was a general named Xun Lu who belonged to Kingdom Wu. Once his troop were chasing Bei Liu, he was stuck in the Ba Gua Zhen from Liang Zhuge.

2015南阳CCPC D - Pick The Sticks dp

D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao

CDOJ 1220 The Battle of Guandu

The Battle of Guandu Time Limit: 6000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) 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 tw