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+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e18+;
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=; i<NN; i++)
{
for(int j=; j<NN; j++)
prevv[i][j]=i;
}
for(int k=; k<NN; k++)
{
for(int i=; i<NN; i++)
{
for(int j=; 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]<)
{
fill(used,used+NN,);
for(int v=i; !used[v]; v=prevv[i][v])
{
used[v]=;
if(v!=n+m+&&prevv[i][v]!=n+m+)
{
if(v>n) E[prevv[i][v]][v-n]++;
else E[v][prevv[i][v]-n]--;
}
}
cout<<"SUBOPTIMAL"<<endl;
for(int x=; x<=n; x++)
{
for(int y=; 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=,t=n+m+;
NN=t+;
int f=;
for(int i=; i<=n; i++)
scanf("%d%d%d",&b[i].x,&b[i].y,&b[i].cou);
for(int i=; i<=m; i++)
scanf("%d%d%d",&c[i].x,&c[i].y,&c[i].cou);
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
scanf("%d",&E[i][j]);
}
for(int i=; i<NN; i++)
fill(g[i],g[i]+NN,inf);
for(int j=; j<=m; j++)
{
int sum=;
for(int i=; i<=n; i++)
{
int d=abs(b[i].x-c[j].x)+abs(b[i].y-c[j].y)+;
g[i][j+n]=d;
if(E[i][j]>) g[j+n][i]=-d;
sum+=E[i][j];
}
if(sum>) g[n+m+][j+n]=;
if(sum<c[j].cou) g[j+n][n+m+]=;
}
solve();
return ;
}

消负圈法求最小费用最大流

												

POJ 2135.Farm Tour 消负圈法最小费用最大流的更多相关文章

  1. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  2. poj 2135 Farm Tour 【无向图最小费用最大流】

    题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...

  3. hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流

    /** 题目:hdu3667 Transportation 拆边法+最小费用最大流 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 题意:n个城市由 ...

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

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  5. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

  6. POJ 2135 Farm Tour(最小费用最大流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

  7. POJ 3422 Kaka&#39;s Matrix Travels (最小费用最大流)

    POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...

  8. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

  9. POJ 2135 Farm Tour 最小费用流

    两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...

随机推荐

  1. 微信小程序商品筛选,侧方弹出动画选择页面

    https://blog.csdn.net/qq_36538012/article/details/85110641

  2. 大型运输行业实战_day11_2_事务理论与实际生产配置事务管理

    1.什么是事务(Transaction:tx) 数据库的某些需要分步完成,看做是一个整体(独立的工作单元),不能分割,要么整体成功,要么整体生效.“一荣俱荣,一损俱损”,最能体现事务的思想.案例:银行 ...

  3. DNS隧道 iodns

    通过iodns这个工具也能搭建DNS隧道 iodns的优点: 对下行数据不进行编码,速度快 支持多平台 最大16个并发连接 强制密码设定 iodns创建的DNS隧道网段不能喝服务器,客户端同一网段,比 ...

  4. JMeter学习(五)集合点(转载)

    转载自 http://www.cnblogs.com/yangxia-test JMeter也有像LR中的集合点,本篇就来介绍下JMeter的集合点如何去实现. JMeter里面的集合点通过添加定时器 ...

  5. day31 粘包问题

    TCP粘包问题 cmd客户端代码 import socket import struct import socket import json c = socket.socket() c.connect ...

  6. PHP实现curl和snoopy类模拟登陆方法

    Snoopy.class.php下载 方法/步骤   第一种:使用snoopy类实现模拟登陆 1.在网上下载一个Snoopy.class.php的文件   2.代码实现: <?php set_t ...

  7. eclipse中没有tomcat小猫

    安装了tomcat,按网上的说明也使用了tomcatPluginV331 配置文件,还是没有小猫,后来我发现,网上的tomcatPluginV331 针对eclipse 4.4版本,所以应该是插件的版 ...

  8. pta l2-20(功夫传人)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805059118809088 题意:给定n个人,编号0-n-1, ...

  9. Maven 标签

    scope 1.compile:默认值 他表示被依赖项目需要参与当前项目的编译,还有后续的测试,运行周期也参与其中,是一个比较强的依赖.打包的时候通常需要包含进去 2.test:依赖项目仅仅参与测试相 ...

  10. Codeforces Beta Round #32 (Div. 2, Codeforces format)

    Codeforces Beta Round #32 (Div. 2, Codeforces format) http://codeforces.com/contest/32 A #include< ...