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. 大型运输行业实战_day01_1_业务分析

    1.业务分析 发展历史:  上车收费-->车站买票(相当于先收钱后上车)-->站务系统--->联网售票 2.项目结构 3.开发流程分析 1.业务分析            图文并茂  ...

  2. Map、Set、List 集合 差别 联系

    提到集合之前,先说说数组Array和集合的区别:   (1)数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型)   (2)JAVA集合可以存储和操作数目不固定的一组数据. ( ...

  3. python网络编程之开启进程的方式

    标签(空格分隔): 开启进程的方式 multiprocessing模块介绍: python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在pyth ...

  4. 站点防火墙频率api php案例

    <?php $apiHost = "http://35.201.139.124/api2/site/index.php"; $router = "token&quo ...

  5. 用户维护 UI 检验周期更新逻辑

    在SAP系统中建立UI,供用户维护物料组对应的检验周期,FP按照物料组对应的物料编码取UI维护的检验周期进FP系统规划. --物料组对应检验周期维护表(新UI)add by landor on 201 ...

  6. vue打包后,接口请求404的完美解决方案

    在开发环境中,和后台对接为了解决跨域问题,使用了代理,也就是vue的proxyTable,但是打包放到生产环境中去时,接口请求不到,404,原因是开发环境的代理并不能用到生产环境,但是直接在请求接口是 ...

  7. as3.0视频的快进有拖动条

    package com{ import flash.display.MovieClip; import flash.events.MouseEvent; import fl.video.FLVPlay ...

  8. NAT和Proxy的区别

    在internet共享上网技术上,一般有两种方式,一种是proxy代理型,一种是NAT网关型,关于两者的区别与原理,身边很多人都不是很明白,下面我来讲讲我的理解,如有不对的,欢迎指正. 1.先说应用例 ...

  9. NumPy 数学函数

    NumPy 数学函数 NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等. 三角函数 NumPy 提供了标准的三角函数:sin().cos().tan(). 实例 ...

  10. Codeforces Round #518 (Div. 2) [Thanks, Mail.Ru!]

    Codeforces Round #518 (Div. 2) [Thanks, Mail.Ru!] https://codeforces.com/contest/1068 A #include< ...