• POJ2175 Evacuation Plan


    Evacuation Plan
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4617   Accepted: 1218   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

    费用流消圈算法。

    根据已有的残量矩阵建图,由于残量可以直接从图上读到,所以不需要在边里存容量。

    SPFA判断是否有负环,有则处理。

    有点没看懂,姑且抄份代码慢慢研究

      1 #include<iostream>
      2 #include<stdio.h>
      3 #include<stdlib.h>
      4 #include<string.h>
      5 #include<algorithm>
      6 #include<queue>
      7 #define LL long long
      8 using namespace std;
      9 const int INF=1e9;
     10 const int mxn=210*2;
     11 inline int read(){
     12     int sum=0,flag=1;char ch=getchar();
     13     while(ch!='-'&&(ch>'9'||ch<'0'))ch=getchar();
     14     if(ch=='-'){flag=-1;ch=getchar();}
     15     while(ch<='9'&&ch>='0'){sum=sum*10+ch-'0';ch=getchar();}
     16     return sum*flag;
     17 }
     18 struct edge{
     19     int u,v,nxt,w;
     20 }e[mxn*mxn*2];
     21 int hd[mxn],mct=1;
     22 void add_edge(int u,int v,int w){
     23 //    printf("add:%d to %d :%d
    ",u,v,w);
     24     e[++mct].u=u;e[mct].v=v;e[mct].nxt=hd[u];e[mct].w=w;hd[u]=mct;return;
     25 }
     26 int n,m,S,T;
     27 int mp[mxn][mxn];
     28 int dis[mxn];
     29 int pre[mxn];
     30 int cnt[mxn];
     31 bool inq[mxn];
     32 bool SPFA(){
     33     memset(dis,0x3f,sizeof dis);
     34     memset(inq,0,sizeof inq);
     35     memset(cnt,0,sizeof cnt);
     36     queue<int>q;
     37     q.push(T);
     38     dis[T]=0;inq[T]=1;pre[T]=0;cnt[T]++;
     39     bool flag=1;
     40     int v;
     41     while(!q.empty() && flag){
     42         int u=q.front();q.pop();inq[u]=0;
     43         for(int i=hd[u];i;i=e[i].nxt){
     44             v=e[i].v;
     45             if(dis[v]>dis[u]+e[i].w){
     46                 dis[v]=dis[u]+e[i].w;
     47                 pre[v]=u;
     48                 if(!inq[v]){
     49                     q.push(v);
     50                     inq[v]=1; cnt[v]++;
     51                     if(cnt[v]>=n+m+3){
     52                         flag=0;
     53                         break;
     54                     }
     55                 }
     56             }
     57         }
     58     }
     59     if(flag)printf("OPTIMAL
    ");
     60     else{
     61         printf("SUBOPTIMAL
    ");
     62         memset(inq,0,sizeof inq);
     63         int s=v;
     64         while(1){
     65             if(!inq[s])inq[s]=1,s=pre[s];
     66             else break;
     67         }
     68         memset(inq,0,sizeof inq);
     69         while(!inq[s]){
     70             inq[s]=1;
     71             int p=pre[s];
     72             if(p>n && s!=T) mp[s][p]--;
     73             else if(s>n && p!=T) mp[p][s]++;
     74             s=pre[s];
     75         }
     76         int ed=n+m;
     77         for(int i=1;i<=n;i++){//输出可行解 
     78             for(int j=n+1;j<=ed;j++){
     79                 if(j!=n+1)printf(" ");
     80                 printf("%d",mp[i][j]);
     81             }
     82             printf("
    ");
     83         }
     84     }
     85     return 0;
     86 }
     87 int x[mxn],y[mxn],w[mxn],in[mxn];
     88 void Build(){
     89     memset(hd,0,sizeof hd);
     90     memset(in,0,sizeof in);
     91     mct=1;
     92     int i,j;
     93     for(i=1;i<=n;i++)
     94         for(j=n+1;j<=n+m;j++){
     95             int v=abs(x[i]-x[j])+abs(y[i]-y[j])+1;//代价
     96 //            printf("%d ",v);
     97             add_edge(i,j,v);
     98             if(mp[i][j])add_edge(j,i,-v); 
     99             in[j]+=mp[i][j];
    100         }
    101 //    printf("
    ");
    102     for(i=n+1;i<=n+m;i++){
    103         if(in[i]) add_edge(T,i,0);
    104         if(in[i]<w[i])add_edge(i,T,0);
    105     }
    106     return;
    107 }
    108 int main(){
    109     int i,j;
    110     while(scanf("%d%d",&n,&m)!=EOF){
    111         int ed=n+m;T=0;
    112         for(i=1;i<=ed;i++){
    113             x[i]=read();y[i]=read();w[i]=read();
    114         }
    115         for(i=1;i<=n;i++)
    116             for(j=n+1;j<=ed;j++)
    117                 mp[i][j]=read();
    118         Build();
    119         SPFA();
    120     }
    121     return 0;
    122 }
  • 相关阅读:
    关于xutils中的BitmapUtil实现简单的缓存和下载
    自定义spinner
    Dart格式化输出
    使用 dsc_extractor 导出 dyld_shared_cache_arm64
    iOS11 获取手机已安装应用列表
    恢复二进制文件中的block符号表
    iOS 反反注入 修改__RESTRICT,__restrict工具
    Node的调试
    UVa10131
    UVa116
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6241372.html
Copyright © 2020-2023  润新知