Language:
Evacuation Plan
Description ![]() 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 Source |
题意:n个办公楼和m个防空洞,每个大楼里有一定人数,每个防空洞有一个最大容量,现在告诉每个大楼的人数和每个防空洞的容量,还告诉一个撤离方案,问是否是最优方案,否则输出更优方案。
思路:很容易想到是最小费用流,但是会超时。这是一个费用流消圈的问题,有这样一个定理:一个费用流是最小费用流的充要条件是这个费用流的残留网络中没有负费用圈。负圈是指费用总和是负数,且每条边的剩余流量大于0的圈。我们先根据已知条件构造出残留网络,在残留网络中从汇点出发用spfa找负圈。我是按照原图建边的,网上好多是简化了的。
代码:
#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 205;
const int MAXM = 200010;
struct Node
{
int x,y,z;
}node[222];
struct Edge
{
int to,next,cap,flow,cost;
}edge[MAXM];
int times[205];
int mp[205][205];
int sum[205];
int ans[105][105];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N,n,m;
void init(int n)
{
N=n;
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int flow,int cost)
{
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].cost=cost;
edge[tol].flow=flow;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].cost=-cost;
edge[tol].flow=-flow;
edge[tol].next=head[v];
head[v]=tol++;
}
int spfa()
{
int i,j;
memset(vis,false,sizeof(vis));
memset(pre,-1,sizeof(pre));
memset(dis,INF,sizeof(dis));
memset(times,0,sizeof(times));
queue<int>Q;
vis[n+m+1]=true;
dis[n+m+1]=0;
Q.push(n+m+1);
times[n+m+1]++;
while (!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=false;
for (i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if (edge[i].cap-edge[i].flow>0&&dis[v]>dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=u;
if (!vis[v])
{
vis[v]=true;
times[v]++;
Q.push(v);
if (times[v]>n+m+2) return v;
}
}
}
}
return -1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j;
while (~scanf("%d%d",&n,&m))
{
for (i=1;i<=n+m;i++)
scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].z);
for (i=1;i<=n;i++)
{
for (j=n+1;j<=n+m;j++)
{
mp[i][j-n]=abs(node[i].x-node[j].x)+abs(node[i].y-node[j].y)+1;
}
}
init(n+m+2);
memset(sum,0,sizeof(sum));
for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
scanf("%d",&ans[i][j]);
addedge(i,j+n,INF,ans[i][j],mp[i][j]);
sum[j]+=ans[i][j];
}
}
for (i=1;i<=n;i++) addedge(0,i,node[i].z,node[i].z,0);
for (i=1;i<=m;i++) addedge(i+n,n+m+1,node[i+n].z,sum[i],0);
int s=spfa();
if (s==-1) printf("OPTIMAL\n");
else
{
printf("SUBOPTIMAL\n");
memset(vis,false,sizeof(vis));
int ss=s;
while (1)
{
if (!vis[ss])
{
vis[ss]=true;
ss=pre[ss];
}
else
{
s=ss;
break;
}
}
do{
int from=pre[ss];
int to=ss;
if (from<=n&&to>n) ans[from][to-n]++;
if (to<=n&&from>n) ans[to][from-n]--;
ss=pre[ss];
}while (s!=ss);
for (i=1;i<=n;i++)
{
printf("%d",ans[i][1]);
for (j=2;j<=m;j++)
printf(" %d",ans[i][j]);
printf("\n");
}
}
}
return 0;
}