HDU

来源:互联网 发布:nba新秀数据库 编辑:程序博客网 时间:2024/06/01 14:21

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4289

Control

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3514    Accepted Submission(s): 1479


Problem Description
  You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
  The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.
  You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.
  It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you must identify a set of cities, that:
  * all traffic of the terrorists must pass at least one city of the set.
  * sum of cost of controlling all cities in the set is minimal.
  You may assume that it is always possible to get from source of the terrorists to their destination.
------------------------------------------------------------
1 Weapon of Mass Destruction
 

Input
  There are several test cases.
  The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.
  The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.
  The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 107.
  The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.
  Please process until EOF (End Of File).
 

Output
  For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
  See samples for detailed information.
 

Sample Input
5 65 35234121 55 42 32 44 32 1
 

Sample Output
3
 

Source
2012 ACM/ICPC Asia Regional Chengdu Online

题目大意:

有一个起点和终点,有m条边n个点,每个点上都有权值。把哪些点去掉使得起点和终点不联通且去掉点的权值和最小。

题解:

最小割的应用,就是把边权值改成个点权值,那就只能拆点建边了,然后求最大流=最小割。关键还是怎么建边。拆点后u->u'的权值就是该点的权值,加入u和v相连,那么就建边:u->u’->v->v'->u  u'->v的权值为INF,最后求起点->终点‘ 的最大流。

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <algorithm>#include <cmath>#define INF 0x3f3f3f3fusing namespace std;typedef long long LL;const int maxn=1e5+7;int cas=1,T;int d[maxn];///标记数组int cur[maxn];int start,tend;///源点和汇点struct node///建边{    int to,cap,next;}eage[300000];int head[maxn];bool vis[maxn];///标记int cnt;///建边函数void ADD(int u,int v,int c){    eage[cnt].to=v;    eage[cnt].cap=c;    eage[cnt].next=head[u];    head[u]=cnt++;}bool BFS(){    memset(d,-1,sizeof(d));    int Q[maxn*2];///队列    int Thead=0,Ttail=0;    Q[Ttail++]=start;    d[start]=0;///标记    while(Thead<Ttail)    {        int x=Q[Thead];        if(x==tend)return true;        for(int i=head[x];i!=-1;i=eage[i].next)        {            int temp=eage[i].to;            if(d[temp]==-1&&eage[i].cap>0)///没有标记,且可行流大于0            {                d[temp]=d[x]+1;                Q[Ttail++]=temp;            }        }        Thead++;    }    return false;///汇点是否成功标号,也就是说是否找到曾广路}int DFS(int x,int cap){    if(x==tend)return cap;    int flow = 0 , f;    for(int i=head[x];i!=-1;i=eage[i].next)    {        int temp = eage[i].to;        if(d[temp]==d[x]+1&&eage[i].cap)        {            f=DFS(temp,min(cap-flow,eage[i].cap));            eage[i].cap -= f;            eage[i^1].cap+=f;            flow+=f;            if(flow==cap)break;        }    }    if(!flow)d[x]=-2;///防止重搜,剪枝。否则会超时    return flow;}int maxflow(){    int flow=0,f;    while(BFS())    {        while((f=DFS(start,INF))>0)            flow+=f;    }    return flow;}int main(){    int n,m;    while(~scanf("%d%d",&n,&m)){        cnt=0;        memset(head,-1,sizeof(head));        int x,u,v;        //int tmax=-INF,tmin=INF;        //start=tend=1;        scanf("%d%d",&start,&tend);        tend+=n;//        ///每个点的坐标//        for(int i=1;i<=n;i++)//        {//            scanf("%d%d",&x,&y);//            if(x<=tmin)start=i,tmin=x;//            if(x>=tmax)tend=i,tmax=x;//        }        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            ADD(i,i+n,x);            ADD(i+n,i,0);        }        while(m--)        {            scanf("%d%d",&u,&v);///u<->v的流量是w(双向边)            ADD(u+n,v,INF);            ADD(v,u+n,0);            ADD(v+n,u,INF);            ADD(u,v+n,0);        }        int ans=maxflow();        printf("%d\n",ans);    }    return 0;}



原创粉丝点击