poj 2175

      一道很好的题目,看似是费用流,实际上不需要用费用流,用Floyd算法就可以了,当然在Floyd算法中要做一些处理。

      开始我一直在想用费用流,可是看了答案后才明白用Floyd算法找出并消去一个负环就可以了。因为题目说“Your plan need not be optimal itself, but must be valid and better than The City Council's one”,也就是说我们不必求出最佳方案,只要提出比现有方案更好的方案就行了,所以用费用流就麻烦了。构图时要想着Floyd算法所需的存储图的数据结构,即矩阵,所以我们要构造一个距离矩阵。包含的点出了市政大楼和防空洞,还应该包括超级汇点,否则就有可能漏掉一些负环。

      更详细可以参考《挑战程序设计竞赛》 人民邮电出版社 P240。

代码(C++):

#include <cstdlib>
#include <iostream>

#define MAX 150
#define INF (1<<30)
using namespace std;

//#define LOCAL

int x[MAX],y[MAX],b[MAX],p[MAX],q[MAX],c[MAX],e[MAX][MAX],g[MAX*2][MAX*2],pre[MAX*2][MAX*2];
bool used[MAX*2];

bool floyd(int v,int n)
{
     int i,j,k,t;
     for(k=0;k<v;k++)
     {
         for(i=0;i<v;i++)
         {
             for(j=0;j<v;j++)
             {
                 if(g[i][k]!=INF&&g[k][j]!=INF&&g[i][j]>g[i][k]+g[k][j])
                 {
                      g[i][j]=g[i][k]+g[k][j];
                      pre[i][j]=pre[i][k];
                      if(i==j&&g[i][j]<0)   //因为之前未将g[i][i]赋值为0,
                      {                     //所以此处要判断g[i][j]<0是否成立    
                          fill(used,used+v,false);
                          for(t=i;!used[t];t=pre[t][j])
                          {
                              used[t]=true;                              
                              if(g[t][pre[t][j]]>0) e[t][pre[t][j]-n]++;    //注意下标的对应 
                              else if(g[t][pre[t][j]]<0) e[pre[t][j]][t-n]--;                          
                          }
                          return true; 
                      }                  
                 }          
             }            
         }             
     }
     return false;
}

int main(int argc, char *argv[])
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int n,m,i,j,v,w,sum;
    bool tag;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        v=n+m+1;       //municipal buildings:0~n-1   fallout shelters:n~n+m    超级汇点:n+m 
        for(i=0;i<n;i++) scanf("%d %d %d",&x[i],&y[i],&b[i]);
        for(i=0;i<m;i++) scanf("%d %d %d",&p[i],&q[i],&c[i]);
        for(i=0;i<n;i++)
        {                        
            for(j=0;j<m;j++)  scanf("%d",&e[i][j]);          
        }
        
        for(i=0;i<v;i++) fill(g[i],g[i]+v,INF);

        for(j=0;j<m;j++)
        {        
            sum=0;                     
            for(i=0;i<n;i++)
            {                                                          
                w=abs(x[i]-p[j])+abs(y[i]-q[j])+1;
                if(e[i][j]<min(b[i],c[j])) g[i][n+j]=w;
                if(e[i][j]>0) g[n+j][i]=-w;  
                sum+=e[i][j];              
            }   
            if(sum<c[j])  g[n+j][n+m]=0;
            if(sum>0)   g[n+m][n+j]=0;    
        }
        for(i=0;i<v;i++)
        {
            for(j=0;j<v;j++)
            {
                pre[i][j]=j;            
            }            
        }
        tag=floyd(v,n);   
        if(tag==true)
        {
            printf("SUBOPTIMAL\n");  
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++) printf("%d%c",e[i][j],j==m-1?'\n':' ');            
            }       
        }else printf("OPTIMAL\n");        
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

题目( http://poj.org/problem?id=2175):

Evacuation Plan
Time Limit: 1000MS Memory Limit: 65536K
    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 D i,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 E i,j separated by spaces. E i,j (0 ≤ E i,j ≤ 1000) is a number of workers that shall evacuate from the i th municipal building to the j th 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值