POJ 2135.Farm Tour 消负圈法最小费用流

Posted GeekZRF

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2135.Farm Tour 消负圈法最小费用流相关的知识,希望对你有一定的参考价值。

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4914   Accepted: 1284   Special Judge

Description

技术分享The City has a number of municipal buildings and a number of fallout shelters that were build specially to hide municipal workers in case of a nuclear war. Each fallout shelter has a limited capacity in terms of a number of people it can accommodate, and there‘s almost no excess capacity in The City‘s fallout shelters. Ideally, all workers from a given municipal building shall run to the nearest fallout shelter. However, this will lead to overcrowding of some fallout shelters, while others will be half-empty at the same time. 

To address this problem, The City Council has developed a special evacuation plan. Instead of assigning every worker to a fallout shelter individually (which will be a huge amount of information to keep), they allocated fallout shelters to municipal buildings, listing the number of workers from every building that shall use a given fallout shelter, and left the task of individual assignments to the buildings‘ management. The plan takes into account a number of workers in every building - all of them are assigned to fallout shelters, and a limited capacity of each fallout shelter - every fallout shelter is assigned to no more workers then it can accommodate, though some fallout shelters may be not used completely. 

The City Council claims that their evacuation plan is optimal, in the sense that it minimizes the total time to reach fallout shelters for all workers in The City, which is the sum for all workers of the time to go from the worker‘s municipal building to the fallout shelter assigned to this worker. 

The City Mayor, well known for his constant confrontation with The City Council, does not buy their claim and hires you as an independent consultant to verify the evacuation plan. Your task is to either ensure that the evacuation plan is indeed optimal, or to prove otherwise by presenting another evacuation plan with the smaller total time to reach fallout shelters, thus clearly exposing The City Council‘s incompetence. 

During initial requirements gathering phase of your project, you have found that The City is represented by a rectangular grid. The location of municipal buildings and fallout shelters is specified by two integer numbers and the time to go between municipal building at the location (Xi, Yi) and the fallout shelter at the location (Pj, Qj) is Di,j = |Xi - Pj| + |Yi - Qj| + 1 minutes. 

Input

The input consists of The City description and the evacuation plan description. The first line of the input file consists of two numbers N and M separated by a space. N (1 ≤ N ≤ 100) is a number of municipal buildings in The City (all municipal buildings are numbered from 1 to N). M (1 ≤ M ≤ 100) is a number of fallout shelters in The City (all fallout shelters are numbered from 1 to M). 

The following N lines describe municipal buildings. Each line contains there integer numbers Xi, Yi, and Bi separated by spaces, where Xi, Yi (-1000 ≤ Xi, Yi ≤ 1000) are the coordinates of the building, and Bi (1 ≤ Bi ≤ 1000) is the number of workers in this building. 

The description of municipal buildings is followed by M lines that describe fallout shelters. Each line contains three integer numbers Pj, Qj, and Cj separated by spaces, where Pi, Qi (-1000 ≤ Pj, Qj ≤ 1000) are the coordinates of the fallout shelter, and Cj (1 ≤ Cj ≤ 1000) is the capacity of this shelter. 

The description of The City Council‘s evacuation plan follows on the next N lines. Each line represents an evacuation plan for a single building (in the order they are given in The City description). The evacuation plan of ith municipal building consists of M integer numbers Ei,j separated by spaces. Ei,j (0 ≤ Ei,j ≤ 1000) is a number of workers that shall evacuate from the ith municipal building to the jthfallout shelter. 

The plan in the input file is guaranteed to be valid. Namely, it calls for an evacuation of the exact number of workers that are actually working in any given municipal building according to The City description and does not exceed the capacity of any given fallout shelter. 

Output

If The City Council‘s plan is optimal, then write to the output the single word OPTIMAL. Otherwise, write the word SUBOPTIMAL on the first line, followed by N lines that describe your plan in the same format as in the input file. Your plan need not be optimal itself, but must be valid and better than The City Council‘s one.

Sample Input

3 4
-3 3 5
-2 -2 6
2 2 5
-1 1 3
1 1 4
-2 -2 7
0 -1 3
3 1 1 0
0 0 6 0
0 3 0 2

Sample Output

SUBOPTIMAL
3 0 1 1
0 0 6 0
0 4 0 1

题目链接:http://poj.org/problem?id=2175
题意:有n栋建筑,m个防空洞,现在市政府给出了一个逃生方案,问有没有一个更好的方案,有的话,输出方案。
思路:最小费用最大流。流量为人数,花费为距离。不知道是不是写矬了,用连续最短路求最小费用最大流一直TLE。改成用消负圈法,即不断的消去负圈而得到最小费用流。应为是任意两点间的最短路径并且含有负边,用Floyd算法。
代码:
技术分享
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define PI acos(-1.0)
const int maxn=3e2+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
const ll INF=1e18+7;
int n,m;
int NN;
struct node
{
    int x,y;
    int cou;
} b[maxn],c[maxn];
int E[maxn][maxn];
int g[maxn][maxn];
int prevv[maxn][maxn];
int used[maxn];
void solve()
{
    for(int i=1; i<NN; i++)
    {
        for(int j=1; j<NN; j++)
            prevv[i][j]=i;
    }
    for(int k=1; k<NN; k++)
    {
        for(int i=1; i<NN; i++)
        {
            for(int j=1; j<NN; j++)
            {
                if(g[i][j]>g[i][k]+g[k][j])
                {
                    g[i][j]=g[i][k]+g[k][j];
                    prevv[i][j]=prevv[k][j];
                    if(i==j&&g[i][j]<0)
                    {
                        fill(used,used+NN,0);
                        for(int v=i; !used[v]; v=prevv[i][v])
                        {
                            used[v]=1;
                            if(v!=n+m+1&&prevv[i][v]!=n+m+1)
                            {
                                if(v>n) E[prevv[i][v]][v-n]++;
                                else E[v][prevv[i][v]-n]--;
                            }
                        }
                        cout<<"SUBOPTIMAL"<<endl;
                        for(int x=1; x<=n; x++)
                        {
                            for(int y=1; y<m; y++)
                                cout<<E[x][y]<<" ";
                            cout<<E[x][m]<<endl;
                        }
                        return;
                    }
                }
            }
        }
    }
    cout<<"OPTIMAL"<<endl;
}
int main()
{
    scanf("%d%d",&n,&m);
    int s=0,t=n+m+1;
    NN=t+1;
    int f=0;
    for(int i=1; i<=n; i++)
        scanf("%d%d%d",&b[i].x,&b[i].y,&b[i].cou);
    for(int i=1; i<=m; i++)
        scanf("%d%d%d",&c[i].x,&c[i].y,&c[i].cou);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            scanf("%d",&E[i][j]);
    }
    for(int i=0; i<NN; i++)
        fill(g[i],g[i]+NN,inf);
    for(int j=1; j<=m; j++)
    {
        int sum=0;
        for(int i=1; i<=n; i++)
        {
            int d=abs(b[i].x-c[j].x)+abs(b[i].y-c[j].y)+1;
            g[i][j+n]=d;
            if(E[i][j]>0) g[j+n][i]=-d;
            sum+=E[i][j];
        }
        if(sum>0) g[n+m+1][j+n]=0;
        if(sum<c[j].cou) g[j+n][n+m+1]=0;
    }
    solve();
    return 0;
}
消负圈法求最小费用最大流

以上是关于POJ 2135.Farm Tour 消负圈法最小费用流的主要内容,如果未能解决你的问题,请参考以下文章

网络流(最小费用最大流):POJ 2135 Farm Tour

poj 2135 Farm Tour 最小费最大流

poj 2135: Farm Tour

POJ2135 Farm Tour

POJ2135:Farm Tour——题解

POJ2135 Farm Tour