Evacuation Plan
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 5103 | Accepted: 1329 | Special Judge |
Description
![](https://i-blog.csdnimg.cn/blog_migrate/8735b60087c6eecd9ba982cd501a9516.jpeg)
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 thfallout 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.
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 thfallout 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个避难所,每一个市政大楼都有一定的人数,而每一个避难所也有一定的容量,从某个市政大楼到某个避难所的花费是曼哈顿距离+1,现在委员会给你一个有效的疏散计划,判断还有没有这个计划更优的方案,输出它!!!
分析:很直白的网络流建图.只是会T。点数这么小感觉应该不会T的,可能是多组数据的原因吧......
判断有没有更优的费用流可以利用消圈算法,详细的介绍可以看:传送门.知道了这个定理以后,关键就是如何求出残余网络和负圈。建残余网络可以根据题目给的方案来.为了保证图的连通性,先设置一个汇点T.
1.如果大楼A到避难所B的人数>0,则从B向A连一条边权为负的AB距离的边.
2.若大楼A的人没有全部到B,则从A向B连一条边权为正的AB距离的边.
3.统计每个避难所来了多少个人.如果第i个避难所的人>0,从T向i连一条边权为0的边.
4.如果第i个避难所的人小于它的容量,从i向T连一条边权为0的边.
接下来利用spfa求负环,记录前驱.求出负环以后,将负环上的所有边增广1就是答案了.如果一条边是从市政大楼u指向避难所v,那么u到v的答案++,如果是避难所指向市政大楼,那么答案--.最后输出答案即可.
建图主要利用的是正向边和反向边和流量的关系.spfa求负环要注意入队次数的上限不是n,而是n+m.
#include <cstdio> #include <cmath> #include <queue> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int inf = 0x3f3f3f3f; int n,m,d[110][110],T,ans[110][110],sum[1010],cnt[10010],pre[10010],vis[10010]; int dist[1010],head[100010],to[100010],nextt[100100],tot = 1,w[100010],vis2[10010]; struct node { int x,y,num; } people[110],building[110]; void add(int x,int y,int z) { w[tot] = z; to[tot] = y; nextt[tot] = head[x]; head[x] = tot++; } int spfa(int S) { queue <int> q; memset(dist,inf,sizeof(dist)); memset(vis,0,sizeof(vis)); vis[S] = 1; cnt[S]++; dist[S] = 0; q.push(S); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for (int i = head[u]; i; i = nextt[i]) { int v = to[i]; if (dist[v] > dist[u] + w[i]) { dist[v] = dist[u] + w[i]; pre[v] = u; if (!vis[v]) { vis[v] = 1; q.push(v); if (++cnt[v] >= T) return v; } } } } return -1; } int main() { while (scanf("%d%d",&n,&m) != EOF) { memset(ans,0,sizeof(ans)); memset(sum,0,sizeof(sum)); tot = 1; memset(head,0,sizeof(head)); memset(vis2,0,sizeof(vis2)); memset(d,0,sizeof(d)); memset(cnt,0,sizeof(cnt)); T = n + m + 1; for (int i = 1; i <= n; i++) scanf("%d%d%d",&people[i].x,&people[i].y,&people[i].num); for (int i = 1; i <= m; i++) scanf("%d%d%d",&building[i].x,&building[i].y,&building[i].num); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) d[i][j] = abs(people[i].x - building[j].x) + abs(people[i].y - building[j].y) + 1; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { scanf("%d",&ans[i][j]); if (ans[i][j] != 0) add(j + n,i,-d[i][j]); if (ans[i][j] != people[i].num) add(i,j + n,d[i][j]); sum[j] += ans[i][j]; } for (int i = 1; i <= m; i++) { if (sum[i] > 0) add(T,i + n,0); if (sum[i] < building[i].num) add(i + n,T,0); } int id = spfa(T); if (id == -1) { puts("OPTIMAL"); continue; } else { puts("SUBOPTIMAL"); while (!vis2[id]) { vis2[id] = 1; id = pre[id]; } int st = id; do //不能直接写while { int u = pre[st],v = st; if (u <= n && v > n) ans[u][v - n]++; if (v <= n && u > n) ans[v][u - n]--; st = pre[st]; }while (st != id); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (j != m) printf("%d ",ans[i][j]); else printf("%d",ans[i][j]); } printf("\n"); } } } return 0; }