POJ--3308 -- paratroopers [dinic] bipartite graph vertex coverage + maximum network flow

Source: Internet
Author: User

Link:Http://poj.org/problem? Id = 3308

Question:In the future world, Mars will intrude into the earth, and they will send some paratroopers to destroy the Earth's arsenal, which can be regarded as an M * n matrix. Now they know the landing location of each of them. In order to smash the Martian conspiracy, we need to set up a machine gun in a row or column to destroy the whole row or whole column of the Martian, but it takes a certain amount of money, the cost of each row and each column of machine gun is multiplied by the cost of each row and each column. The minimum cost to make all the Martians disappear.


Ideas:All enemies need to be eliminated, which is the problem of minimum vertex weight coverage in a bipartite graph. To cover all edges and minimize the cost, we need to find the minimum cut. According to the maximum flow minimum cut theorem, the problem is converted to maximizing the stream.

Graph creation:Create a bipartite graph, use rows and columns as vertices, create a Source Vertex and a sink vertex, and The Source Vertex connects a capacity to all row vertices as the arc of the row, all column vertices connect an arc with a capacity of the column to the sink point, and an arc with a capacity of INF between the row and the column for the alien airborne position. Set the set of all row vertices to R, set the set of all column vertices to C, set the source point to SRC, and set the sink point to sink. The graph is divided into three modules: SRC → R, R → C, C → sink. After this graph is created, the R → C capacity is INF and cannot be selected as the minimum cut. Therefore, the minimum cut is the set of SRC → R and C → sink, that is, the rows and columns are selected, make sure that all Martians are eliminated.

Data processing:The minimum cost is the product, so we can take the right of each arc to log (), and sum up the maximum stream, and then exp () is the final answer.


#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 500100#define eps 1e-7#define INF 0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 131#define mod 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct node{    int u,next;    double w;}edge[5000];int dist[200],head[200];int n,m,cnt,l,src,sink;void add_edge(int a,int b,double c){    edge[cnt].u = b;    edge[cnt].w = c;    edge[cnt].next = head[a];    head[a] = cnt++;}bool bfs(){    int i,j;    memset(dist,-1,sizeof(dist));    dist[src] = 1;    queue<int>q;    q.push(src);    while(!q.empty()){        int u = q.front();        q.pop();        for(i=head[u];i!=-1;i=edge[i].next){            int v = edge[i].u;            if(dist[v]==-1&&edge[i].w>eps){                dist[v] = dist[u] + 1;                q.push(v);            }        }    }    if(dist[sink]!=-1)  return true;    return false;}double dfs(int u,double delta){    int i,j;    if(u==sink) return delta;    double dd,ret = 0;    for(i=head[u];i!=-1;i=edge[i].next){        int v = edge[i].u;        if(dist[v]==dist[u]+1&&edge[i].w>eps){            dd = dfs(v,min(edge[i].w,delta));            edge[i].w -= dd;            edge[i^1].w += dd;            delta -= dd;            ret += dd;        }    }    return ret;}int main(){    int i,j,t;    double a;    int b,c;    scanf("%d",&t);    while(t--){        scanf("%d%d%d",&m,&n,&l);        memset(head,-1,sizeof(head));        src = 0;        sink = n + m + 1;        cnt = 0;        for(i=1;i<=m;i++){            scanf("%lf",&a);            add_edge(src,i,log(a));            add_edge(i,src,0);        }        for(i=m+1;i<=n+m;i++){            scanf("%lf",&a);            add_edge(i,sink,log(a));            add_edge(sink,i,0);        }        for(i=0;i<l;i++){            scanf("%d%d",&b,&c);            add_edge(b,c+m,INF);            add_edge(c+m,b,0);        }        double ans = 0;        while(bfs()){            ans += dfs(src,INF);        }        ans = exp(ans);        printf("%.4lf\n",ans);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.