POJ 3308 Paratroopers 最小割、最大流

畫船聽雨發表於2014-02-17

這道題目的建圖是:從源點到行,在從行到列,最後從列到匯點。然後注意權值,因為這裡讓求的是乘積的最大值,所以要是用EK求最大時,用到的是加法,所以需要用對數轉化一下。log(a)+log(b) = log(a*b),然後再轉化回來就行了啊。

這道題目陣列開小了,但是poj一直返回wa,然後就提交了好多遍才找到原因啊,以後的注意一下。


Paratroopers
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6685   Accepted: 2010

Description

It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the Mars. Recently, the commanders of the Earth are informed by their spies that the invaders of Mars want to land some paratroopers in the × n grid yard of one their main weapon factories in order to destroy it. In addition, the spies informed them the row and column of the places in the yard in which each paratrooper will land. Since the paratroopers are very strong and well-organized, even one of them, if survived, can complete the mission and destroy the whole factory. As a result, the defense force of the Earth must kill all of them simultaneously after their landing.

In order to accomplish this task, the defense force wants to utilize some of their most hi-tech laser guns. They can install a gun on a row (resp. column) and by firing this gun all paratroopers landed in this row (resp. column) will die. The cost of installing a gun in the ith row (resp. column) of the grid yard is ri (resp. ci ) and the total cost of constructing a system firing all guns simultaneously is equal to the product of their costs. Now, your team as a high rank defense group must select the guns that can kill all paratroopers and yield minimum total cost of constructing the firing system.

Input

Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing three integers 1 ≤ m ≤ 50 , 1 ≤ n ≤ 50 and 1 ≤ l ≤ 500 showing the number of rows and columns of the yard and the number of paratroopers respectively. After that, a line with m positive real numbers greater or equal to 1.0 comes where the ith number is ri and then, a line with n positive real numbers greater or equal to 1.0 comes where the ith number is ci. Finally, l lines come each containing the row and column of a paratrooper.

Output

For each test case, your program must output the minimum total cost of constructing the firing system rounded to four digits after the fraction point.

Sample Input

1
4 4 5
2.0 7.0 5.0 2.0
1.5 2.0 2.0 8.0
1 1
2 2
3 3
4 4
1 4

Sample Output

16.0000

EK演算法:

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
//#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 110;
using namespace std;

double c[110][110];
int pre[maxn];
double f[maxn];
int n, m;
double sum;

double spfa(int s, int t)
{
    memset(pre, -1 , sizeof(pre));
    memset(f , 0 , sizeof(f));
    queue<int>q;
    while(!q.empty())
        q.pop();
    q.push(s);
    f[s] = INF;
    pre[s] = 0;
    while(!q.empty())
    {
        int temp = q.front();
        q.pop();
        if(temp == t)
            break;
        for(int i = 0; i <= n+m+1; i++)
        {
            if(c[temp][i] > 0 && pre[i] == -1)
            {
                pre[i] = temp;
                f[i] = min(c[temp][i], f[temp]);
                q.push(i);
            }
        }
    }
    return f[t];
}

void EK(int s, int t)
{
    while(1)
    {
        double temp = spfa(s, t);
        if(temp == 0)
            break;
        for(int i = t; i != s; i = pre[i])
        {
            c[pre[i]][i] -= temp;
            c[i][pre[i]] += temp;
        }
        sum += temp;
    }
}

int main()
{
    int t, l;
    cin >>t;
    while(t--)
    {
        cin >>m>>n>>l;
        memset(c, 0 , sizeof(c));
        for(int i = 1; i <= m; i++)
        {
            double a;
            cin >>a;
            c[0][i] = log(a);
        }
        for(int i = 1; i <= n; i++)
        {
            double a;
            cin >>a;
            c[i+m][n+m+1] = log(a);
        }
        for(int i = 1; i <= l; i++)
        {
            int x, y;
            cin >>x>>y;
            c[x][y+m] = INF;
        }
        sum = 0;
        EK(0, m+n+1);
        cout<<fixed<<setprecision(4)<<exp(sum)<<endl;
    }
    return 0;
}


dinic演算法:

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
//#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 1010;
using namespace std;

int pre[maxn];
int head[maxn];
int cnt;
int n, m;
int deep[maxn];

struct node
{
    int v,next;
    double w;
} edge[maxn*5];

void init()
{
    cnt = 0;
    memset(head , -1 , sizeof(head));
}

void add(int u, int v, double w)
{
    edge[cnt].v = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;

    edge[cnt].v = u;
    edge[cnt].w = 0;
    edge[cnt].next = head[v];
    head[v] = cnt++;
}
bool bfs()//bfs一次找到所有的增廣路
{
    memset(deep, -1 , sizeof(deep));
    queue<int>q;
    while(!q.empty())
        q.pop();
    q.push(0);
    deep[0] = 0;
    while(!q.empty())
    {
        int t = q.front();
        q.pop();
        int p = head[t];
        while(p != -1)
        {
            int v = edge[p].v;
            if(deep[v] == -1 && edge[p].w > 0)
            {
                deep[v] = deep[t]+1;
                q.push(v);
            }
            p = edge[p].next;
        }
    }
    if(deep[n+m+1] == -1)
        return false;
    return true;
}

void fid(int u, int v, double w)//更新反向邊
{
    int p = head[u];
    while(edge[p].v != v)
    {
        p = edge[p].next;
    }
    edge[p].w += w;
}

double dfs(int s, double f)
{
    if(s == n+m+1)
        return f;
    double sum = 0;
    int p = head[s];
    while(p != -1)
    {
        int v = edge[p].v;
        if(deep[v] == deep[s]+1 && edge[p].w > 0)
        {
            double temp = dfs(v, min(f-sum, edge[p].w));//這裡要等於最小的流量,保證所有的可以通過
            sum += temp;
            edge[p].w -= temp;//正向邊流量減少
            fid(v, s, temp);//反向邊流量增加
        }
        p = edge[p].next;
    }
    return sum;
}

double dinic()
{
    double ans=0;
    while(bfs())
    {
        ans += dfs(0,INF);
    }
    return ans;
}

int main()
{
    int t, l;
    cin >>t;
    while(t--)
    {
        cin >>m>>n>>l;
        init();
        for(int i = 1; i <= m; i++)
        {
            double a;
            cin >>a;
            add(0, i, log(a));
        }
        for(int i = 1; i <= n; i++)
        {
            double a;
            cin >>a;
            add(i+m, n+m+1, log(a));
        }
        for(int i = 1; i <= l; i++)
        {
            int x, y;
            cin >>x>>y;
            add(x, y+m, INF);
        }
        double sum;
        sum = dinic();
        cout<<fixed<<setprecision(4)<<exp(sum)<<endl;
    }
    return 0;
}


相關文章