poj 2175(最消费用最大流-消圈法判断是否为最小费用)

这题想了比较久, 一直都有一个想法可是无法确切的提出。 那就是在题目中给出的残留网络中进行某些回流的操作然后得到一个负值就可以减少费用。。。可是我还是没有想到-只要残留网络中存在着负环那么就可以根据负环来更新最小费用。

消圈定理:残留网络里如果存在负费用圈,那么当前流不是最小费用流。(证明略)

那么就可以用spfa找出负环,然后对于环上的边都改变一个流向.  就可以得到结果。

 

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2631   Accepted: 688   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 jth fallout 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

Source

 

 

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
using namespace std;
#define N 210
#define INF 0x3ffffff

struct pos
{
    int x,y,key;
}px[N];

struct node
{
    int to,next,w,c;
}edge[N*N];

int g[N][N];
int n,m;
int s,t;
int cnt,pre[N];
int save[N][N];
int savex[N],savey[N];
int que[N*N];
int point[N],pedge[N];

void add_edge(int u,int v,int w,int c)
{
    edge[cnt].to=v;
    edge[cnt].w=w;
    edge[cnt].c=c;
    edge[cnt].next=pre[u];
    pre[u]=cnt++;
}

int spfa()
{
    int qf=1,qd=0;
    int dis[N],mark[N],num[N];
    memset(point,-1,sizeof(point));
    memset(pedge,-1,sizeof(pedge));
    memset(num,0,sizeof(num));
    int flag=0;
    int key;
    for(int i=0;i<=t;i++)
    {
        dis[i]=INF;
        mark[i]=0;
    }
    dis[t]=0;
    mark[t]=1;
    que[0]=t;
    while(qf>qd)
    {
        int cur=que[qd++];
        mark[cur]=0;
        num[cur]++;
        if( num[cur] > t )
        {
            flag=1; 
            key=cur; // cur是环中的一个点
            break;
        }
        for(int p=pre[cur];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            int w=edge[p].w;
            int c=edge[p].c;
            if(w==0) continue;
            if(dis[v]>dis[cur]+c)
            {
                dis[v]=dis[cur]+c;
                point[v]=cur;
                pedge[v]=p;
                if(mark[v]==0)
                {
                    mark[v]=1;
                    que[qf++]=v;
                }
            }
        }
    }
    if(flag==1)
    {
        int tmp=key;
        int flag1=0;
        int sign[N];
        memset(sign,0,sizeof(sign));
        while(sign[tmp]==0)
        {
            sign[tmp]=1;
            tmp=point[tmp];
        }
        key=tmp;
        while(tmp!=key||flag1==0)
        {
            flag1=1;
            int p=pedge[tmp];
            edge[p].w--;
            edge[p^1].w++; // 在这环上的都要进行这个操作,所以叫消圈法...
            tmp=point[tmp];
        }
        return 1;
    }
    else return 0;
}


int main()
{
    scanf("%d%d",&n,&m);
    s=0;
    t=n+m+1;
    cnt=0;
    memset(pre,-1,sizeof(pre));
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",&px[i].x,&px[i].y,&px[i].key);
        add_edge(s,i,px[i].key,0);
        add_edge(i,s,0,0);
    }
    for(int i=1;i<=m;i++)
    {
        int x,y,key;
        scanf("%d%d%d",&x,&y,&key);
        add_edge(n+i,t,key,0);
        add_edge(t,n+i,0,0);
        for(int j=1;j<=n;j++)
        {
            int tmp=abs(px[j].x-x)+abs(px[j].y-y)+1; // m 与n的 距离
            //save[j][n+i]=tmp;
            add_edge(j,n+i,INF,tmp);
            add_edge(n+i,j,0,-tmp);
        }
    }
    int kk=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            int tmp;
            scanf("%d",&tmp);
            savex[i] += tmp;
            savey[j] += tmp;
            for(int p=pre[i];p!=-1;p=edge[p].next)
            {
                if( edge[p].to==n+j ) // 找到这条边
                {
                    edge[p].w -= tmp;
                    edge[p^1].w += tmp;
                    break;
                }
            }
        }
    for(int i=1;i<=n;i++)
    {
        for(int p=pre[s];p!=-1;p=edge[p].next)
        {
            if(edge[p].to==i)
            {
                edge[p].w-=savex[i];
                edge[p^1].w+=savex[i];
                break;
            }
        }
    }
    for(int i=1;i<=m;i++)
    {
        for(int p=pre[n+i];p!=-1;p=edge[p].next)
        {
            if(edge[p].to==t)
            {
                edge[p].w-=savey[i];
                edge[p^1].w+=savey[i];
                break;
            }
        }
    }
    int sum=spfa();
    
    if(sum==0) printf("OPTIMAL\n");
    else
    {
        printf("SUBOPTIMAL\n");
        
        for(int i=1;i<=n;i++)
        {
            for(int p=pre[i];p!=-1;p=edge[p].next)
            {
                int v=edge[p].to;
                if(v>n)
                {
                    g[i][v-n] = edge[p^1].w;
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                printf("%d ",g[i][j]);
              printf("\n");
        }
    }
    return 0;
}

 

posted @ 2013-03-11 10:26  chenhuan001  阅读(368)  评论(0编辑  收藏  举报