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判断是否有负环,有则处理。

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

 #include<iostream>
 #include<stdio.h>
 #include<stdlib.h>
 #include<string.h>
 #include<algorithm>
 #include<queue>
 #define LL long long
 using namespace std;
 const int INF=1e9;
 *;
 inline int read(){
     ,flag=;char ch=getchar();
     '))ch=getchar();
     ;ch=getchar();}
     +ch-';ch=getchar();}
     return sum*flag;
 }
 struct edge{
     int u,v,nxt,w;
 }e[mxn*mxn*];
 ;
 void add_edge(int u,int v,int w){
 //    printf("add:%d to %d :%d\n",u,v,w);
     e[++mct].u=u;e[mct].v=v;e[mct].nxt=hd[u];e[mct].w=w;hd[u]=mct;return;
 }
 int n,m,S,T;
 int mp[mxn][mxn];
 int dis[mxn];
 int pre[mxn];
 int cnt[mxn];
 bool inq[mxn];
 bool SPFA(){
     memset(dis,0x3f,sizeof dis);
     memset(inq,,sizeof inq);
     memset(cnt,,sizeof cnt);
     queue<int>q;
     q.push(T);
     dis[T]=;inq[T]=;pre[T]=;cnt[T]++;
     ;
     int v;
     while(!q.empty() && flag){
         ;
         for(int i=hd[u];i;i=e[i].nxt){
             v=e[i].v;
             if(dis[v]>dis[u]+e[i].w){
                 dis[v]=dis[u]+e[i].w;
                 pre[v]=u;
                 if(!inq[v]){
                     q.push(v);
                     inq[v]=; cnt[v]++;
                     ){
                         flag=;
                         break;
                     }
                 }
             }
         }
     }
     if(flag)printf("OPTIMAL\n");
     else{
         printf("SUBOPTIMAL\n");
         memset(inq,,sizeof inq);
         int s=v;
         ){
             ,s=pre[s];
             else break;
         }
         memset(inq,,sizeof inq);
         while(!inq[s]){
             inq[s]=;
             int p=pre[s];
             if(p>n && s!=T) mp[s][p]--;
             else if(s>n && p!=T) mp[p][s]++;
             s=pre[s];
         }
         int ed=n+m;
         ;i<=n;i++){//输出可行解
             ;j<=ed;j++){
                 )printf(" ");
                 printf("%d",mp[i][j]);
             }
             printf("\n");
         }
     }
     ;
 }
 int x[mxn],y[mxn],w[mxn],in[mxn];
 void Build(){
     memset(hd,,sizeof hd);
     memset(,sizeof in);
     mct=;
     int i,j;
     ;i<=n;i++)
         ;j<=n+m;j++){
             ;//代价
 //            printf("%d ",v);
             add_edge(i,j,v);
             if(mp[i][j])add_edge(j,i,-v);
             in[j]+=mp[i][j];
         }
 //    printf("\n");
     ;i<=n+m;i++){
         );
         );
     }
     return;
 }
 int main(){
     int i,j;
     while(scanf("%d%d",&n,&m)!=EOF){
         ;
         ;i<=ed;i++){
             x[i]=read();y[i]=read();w[i]=read();
         }
         ;i<=n;i++)
             ;j<=ed;j++)
                 mp[i][j]=read();
         Build();
         SPFA();
     }
     ;
 }

POJ2175 Evacuation Plan的更多相关文章

  1. POJ-2175 Evacuation Plan 最小费用流、负环判定

    题意:给定一个最小费用流的模型,根据给定的数据判定是否为最优解,如果不为最优解则给出一个比给定更优的解即可.不需要得出最优解. 解法:由给定的数据能够得出一个残图,且这个图满足了最大流的性质,判定一个 ...

  2. HDU 3757 Evacuation Plan DP

    跟 UVa 1474 - Evacuation Plan 一个题,但是在杭电上能交过,在UVa上交不过……不知道哪里有问题…… 将施工队位置和避难所位置排序. dp[i][j] 代表前 i 个避难所收 ...

  3. Codeforces Gym 100002 E &quot;Evacuation Plan&quot; 费用流

    "Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  4. POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)

    http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. POJ 2175 Evacuation Plan 费用流 负圈定理

    题目给了一个满足最大流的残量网络,判断是否费用最小. 如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了. 1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在 ...

  6. POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]

    ---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...

  7. UVA 1474 Evacuation Plan

    题意:有一条公路,上面有n个施工队,要躲进m个避难所中,每个避难所中至少有一个施工队,躲进避难所的花费为施工队与避难所的坐标差的绝对值,求最小花费及策略. 解法:将施工队和避难所按坐标排序,可以看出有 ...

  8. poj 2175 Evacuation Plan 最小费用流判定,消圈算法

    题目链接 题意:一个城市有n座行政楼和m座避难所,现发生核战,要求将避难所中的人员全部安置到避难所中,每个人转移的费用为两座楼之间的曼哈顿距离+1,题目给了一种方案,问是否为最优方案,即是否全部的人员 ...

  9. Evacuation Plan-POJ2175最小费用消圈算法

    Time Limit: 1000MS Memory Limit: 65536K Special Judge Description The City has a number of municipal ...

随机推荐

  1. 跟着百度学PHP[6]超级全局变量

    超级全局变量在PHP 4.1.0之后被启用, 是PHP系统中自带的变量,在一个脚本的全部作用域中都可用. 参考文献:http://www.runoob.com/php/php-superglobals ...

  2. ASP.Net MVC开发基础学习笔记:二、HtmlHelper与扩展方法

    一.一个功能强大的页面开发辅助类—HtmlHelper初步了解 1.1 有失必有得 在ASP.Net MVC中微软并没有提供类似服务器端控件那种开发方式,毕竟微软的MVC就是传统的请求处理响应的回归. ...

  3. 能力素质模型咨询工具(Part1)

          之前写过由企业家基本素质想到的文章,里面提及一些能力与素质,以下有内容也可以参考: 领导职位 表6-1 远见卓识的行为表现 级 别 行 为 表 现 A (1)关注行业的前景和环境的变化, ...

  4. 基础才是重中之重~LazyInitializer.EnsureInitialized对属性实现化的性能优化

    回到目录 LazyInitializer.EnsureInitialized是frameworks4.0引入的新东西,实现对属性延时初始化的功能,它作用在System.Threading命名空间下,所 ...

  5. SQL Server数据库性能优化(二)之 索引优化

    参考文献 http://isky000.com/database/mysql-performance-tuning-index 原文作者是做mysql 优化的     但是我觉得  在索引方面    ...

  6. Android杂谈--HTC等手机接收不到UDP广播报文的解决方案

    最近遇到个问题,在android手机上发送UDP报文的时候,HTC等机型(测试用HTC new one)接收不到广播报文,而其他的samsung, huawei, xiaomi, nexus等等均没有 ...

  7. kettle初探

    Kettle是Pentaho的一个组件,主要用于数据库间的数据迁移,到我用过的4.2版,还不支持noSQL,不知道4.4是不是支持了. Kettle自己有三个主要组件:Spoon,Kitchen,Pa ...

  8. [3D跑酷] UI事件处理系统

    在我们的Unity游戏项目中,GUI的表现采用NGUI.记录一下我们的处理方式: 需要解决的问题 1.需要处理大量按钮的点击事件 2.需要处理界面跳转事件 3.需要处理界面元素更新事件 解决方案 GU ...

  9. ED/EP系列1《简介》

    电子存折(ED:ElectronicDeposit)一种为持卡人进行消费.取现等交易而设计的支持个人识别码(PIN)保护的金融IC卡应用.它支持圈存.圈提.消费和取现等交易. 电子钱包(EP:Elec ...

  10. JSON的使用

    最近在项目中大量的使用了JSON, 发现JSON和XML的功能相近,都是一种数据传输格式.只是与XML相比JSON显得更加轻量级,使用也更加容易. JSON依赖的第三方jar包: commons-be ...