码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2

时间:2017-06-17 12:07:25      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:arm   number   can   ref   connect   continue   cat   cti   nim   

https://www.luogu.org/problem/show?pid=2944

题目描述

Wisconsin has had an earthquake that has struck Farmer John‘s farm! The earthquake has damaged some of the pastures so that they are unpassable. Remarkably, none of the cowpaths was damaged.

As usual, the farm is modeled as a set of P (1 <= P <= 3,000)

pastures conveniently numbered 1..P which are connected by a set of C (1 <= C <= 20,000) non-directional cowpaths conveniently

numbered 1..C. Cowpath i connects pastures a_i and b_i (1 <= a_i <= P; 1 <= b_i <= P). Cowpaths might connect a_i to itself or perhaps might connect two pastures more than once. The barn is located in pasture 1.

A total of N (1 <= N <= P) cows (in different pastures) sequentially contacts Farmer John via moobile phone with an integer message report_j (2 <= report_j <= P) that indicates that pasture report_j is undamaged but that the calling cow is unable to return to the barn from pasture report_j because she could not find a path that does not go through damaged pastures.

After all the cows report in, determine the minimum number of

pastures that are damaged.

地震袭击了威斯康星州,一些牧场被摧毁了.

一共有P个牧场.由C条双向路连接.两个牧场间可能有多条路.一条路也可能连接相同的牧场.牛棚坐落在牧场1.

N (1 <= N <= P) 只奶牛打来了求救电话,说她们的农场没有被摧毁,但是已经无法到达牛棚. 求出最少可能有多少牧场被摧毁.

输入输出格式

输入格式:

 

  • Line 1: Three space-separated integers: P, C, and N

  • Lines 2..C+1: Line i+1 describes cowpath i with two integers: a_i and b_i

  • Lines C+2..C+N+1: Line C+1+j contains a single integer: report_j

 

输出格式:

 

  • Line 1: One number, the minimum number of damaged pastures.

 

输入输出样例

输入样例#1:
5 5 2 
1 2 
2 3 
3 5 
2 4 
4 5 
4 
5 
输出样例#1:
1 

说明

Only pasture 2 being damaged gives such a scenario.

 

拆点!!!

#include<queue>
#include<cstdio>
#define N 3101
#define M 21001

using namespace std;

const int inf=2e9;

int n,c,p;
int src,decc,ans;
int front[N*2],to[M*5],nxt[M*5],cap[M*5],tot=1;
int cnt[N*2],lev[N*2];

queue<int>q;

void add(int u,int v,int w)
{
    to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; cap[tot]=w;
    to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; cap[tot]=0;
}
bool bfs()
{
    for(int i=0;i<=p*2+1;i++) 
     lev[i]=-1,cnt[i]=front[i];
    while(!q.empty()) q.pop();
    q.push(src); lev[src]=0;
    int now;
    while(!q.empty())
    {
        now=q.front(); q.pop();
        for(int i=cnt[now];i;i=nxt[i])
            if(lev[to[i]]==-1 && cap[i])
            {
                lev[to[i]]=lev[now]+1;
                q.push(to[i]);
                if(to[i]==decc) return true; 
            }
    }
    return false;
}
int dinic(int now,int flow)
{
    if(now==decc) return flow;
    int delta,rest=0;
    for(int &i=cnt[now];i;i=nxt[i])
    {
        if(cap[i] && lev[to[i]]>lev[now])
        {
            delta=dinic(to[i],min(flow-rest,cap[i]));
            if(delta) 
            {
                rest+=delta;
                cap[i]-=delta; cap[i^1]+=delta;
                if(rest==flow) break;
            }
        }
    }
    if(rest!=flow) lev[now]=-1;
    return rest;
}
int main()
{
    scanf("%d%d%d",&p,&c,&n);
    decc=1;
    int u,v;
    for(int i=2;i<=p;i++) add(i<<1|1,i<<1,1);
    for(int i=1;i<=c;i++)
    {
        scanf("%d%d",&u,&v);
        if(u==v) continue;
        if(u==1) add(v<<1,1,inf);
        else if(v==1) add(u<<1,1,inf);
        else
        {
            add(u<<1,v<<1|1,inf);
            add(v<<1,u<<1|1,inf);
        } 
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&u);
        add(src,u<<1,inf);
    }
    while(bfs()) 
     ans+=dinic(src,inf);
    printf("%d",ans);
}

 

洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2

标签:arm   number   can   ref   connect   continue   cat   cti   nim   

原文地址:http://www.cnblogs.com/TheRoadToTheGold/p/7039726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!