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 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.
题意:
N个点建筑,M个避难所,要把这N个建筑的人全部转移到避难所里,求比输入中给的方案转移时间更短的方案,不要求新的方案是最优方案(如果没有更优方案输出OPTIMAL,如果有就输出SUBOPTIMAL和新的方案)。
解题思路:
用ford_fulkerson用求出最优解然后和原方案对比
这样做肯定是不行的,会TLE。
消圈吧(存在负圈代表可以在不更改流量的情况下减少费用)。
构造残余网络:
所有的建筑物i和避难所j,连接ij,边权为正的距离。
代码:
<pre name="code" class="cpp">#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<queue>
using namespace std;
struct edge
{
int to, next, cost;
}edge[10000000];
int n, m, fir[300], e_max;
inline void add_edge(int from, int to, int cost)
{
edge[e_max].to = to; edge[e_max].cost = cost;
edge[e_max].next = fir[from]; fir[from] = e_max++;
}
inline void init()
{
memset(fir, -1, sizeof(fir));
e_max = 0;
}
int x[100][3], y[100][3], sum[100], ans[100][100];
int pre[300], vis[300], num[300], dis[300];
int g[100][100];
int q[10000000];
int spfa(int sta, int n)
{
int r=1,f=0;
q[0]=sta;
memset(vis, 0, sizeof(vis));
memset(num, 0, sizeof(num));
memset(dis,0x3f3f3f3f,sizeof dis);
vis[sta] = 1;
num[sta]++;
dis[sta] = 0;
while(f<r)
{
int id =q[f++];
vis[id] = 0;
for(int i = fir[id]; i != -1; i = edge[i].next)
if (dis[id] + edge[i].cost < dis[edge[i].to])
{
dis[edge[i].to] = dis[id] + edge[i].cost;
pre[edge[i].to] = id;
if (!vis[edge[i].to])
{
vis[edge[i].to] = 1;
q[r++]=edge[i].to;
if (++num[edge[i].to] > n)
return edge[i].to;
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
for(int i = 0; i < n; i++)
scanf("%d%d%d", &x[i][0], &x[i][1], &x[i][2]);
for(int i = 0; i < m; i++)
scanf("%d%d%d", &y[i][0], &y[i][1], &y[i][2]);
init();
int s = n + m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
g[i][j] = abs(x[i][0] - y[j][0]) + abs(x[i][1] - y[j][1]) + 1;
add_edge(i, n + j, g[i][j]);
}
memset(sum, 0, sizeof(sum));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
int tmp;
scanf("%d", &tmp);
ans[i][j] = tmp;
if (tmp != 0)
add_edge(j + n, i, -g[i][j]);
sum[j] += tmp;
}
for(int i = 0; i < m; i++)
{
if (sum[i] < y[i][2])
add_edge(i + n, s, 0);
if (sum[i] > 0)
add_edge(s, i + n, 0);
}
int id = spfa(s, s + 1);
if (id == -1)
printf("OPTIMAL\n");
else
{
printf("SUBOPTIMAL\n");
int sta = id;
memset(vis, 0, sizeof(vis));
while(true)
{
if (!vis[sta])
{
vis[sta] = true;
sta = pre[sta];
}
else
{
id = sta;
break;
}
}
do
{
int from = pre[sta], to = sta;
if (from < n && to >= n && to < s)
ans[from][to - n]++;
if (to < n && from >= n && from < s)
ans[to][from - n]--;
sta = pre[sta];
}while(sta != id);
for(int i = 0; i < n; i++)
{
printf("%d", ans[i][0]);
for(int j = 1; j < m; j++)
printf(" %d", ans[i][j]);
printf("\n");
}
}
}
return 0;
}