Evacuation Plan
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 jth 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
题目类型:最小费用流(消圈)
算法分析:直接使用最小费用流算法得到的结果是正确的而且是最优方案,但是会TLE。由于本题没有要求最后求得的最终方案一定是最优的,这里使用费用流的消圈算法来求解。消圈定理描述为残余网络里若存在负费用圈,那么当前流不是最小费用流。负圈指的是费用总和为负且每条边剩余流量大于零的圈。若能够在原图中找到一个费用为负的圈的话,那么沿着这个圈流量增加1就能够在不改变原始总流量的基础上使得总费用变得更低,所以只要找到一个负圈即可,先按照输入构造残余网络,然后利用SPFA判断是否存在负圈,若存在负圈则沿着负圈上的点相应修改流量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
/************************************************* Author :supermaker Created Time :2016/4/6 19:04:05 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 200 + 66; struct Node { int v, nxt, c, f, cost; }; struct Point { int x, y, v; }; Point aa[maxn], bb[maxn]; Node edge[maxn*maxn]; int head[maxn], edgelen; int allrow[maxn], allcol[maxn], cap[maxn][maxn]; int Dis (int i, int j) { return abs (aa[i].x - bb[j].x) + abs (aa[i].y - bb[j].y) + 1; } void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); edgelen = 0; } void AddEdge (int cost, int u, int v, int w, int rw = 0) { edge[edgelen].v = v, edge[edgelen].c = w, edge[edgelen].f = 0; edge[edgelen].cost = cost, edge[edgelen].nxt = head[u], head[u] = edgelen++; edge[edgelen].v = u, edge[edgelen].c = rw, edge[edgelen].f = 0; edge[edgelen].cost = -cost, edge[edgelen].nxt = head[v], head[v] = edgelen++; } int dis[maxn], cnt[maxn], inq[maxn], par[maxn]; int N, rt = -1; bool spfa (int s) { for (int i = 0; i <= N; i++) { dis[i] = INF; inq[i] = cnt[i] = 0; par[i] = -1; } dis[s] = 0; queue <int> qu; qu.push (s); inq[s]++, cnt[s]++; while (!qu.empty ()) { int u = qu.front (); qu.pop (); inq[u]--; if (cnt[u] > N) { rt = u; return false; } for (int i = head[u]; i != -1; i = edge[i].nxt) { int v = edge[i].v; if (edge[i].c && dis[u] < INF && dis[v] > dis[u] + edge[i].cost) { dis[v] = dis[u] + edge[i].cost; par[v] = u; if (!inq[v]) { qu.push (v); inq[v]++, cnt[v]++; } } } } return true; } int n, m; void SS (int s) { if (spfa (s)) puts ("OPTIMAL"); else { puts ("SUBOPTIMAL"); int sta = rt; memset (inq, 0, sizeof (inq)); while (1) { if (!inq[sta]) { inq[sta]++, sta = par[sta]; } else { rt = sta; break; } } do { int u = par[sta], v = sta; if (u <= n && v > n) cap[u][v-n]++; if (v <= n && u > n) cap[v][u-n]--; sta = par[sta]; } while (sta != rt); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (j == 1) printf ("%d", cap[i][j]); else printf (" %d", cap[i][j]); } puts (""); } } } int main() { //CFF; //CPPFF; while (scanf ("%d%d", &n, &m) != EOF) { for (int i = 1; i <= n; i++) scanf ("%d%d%d", &aa[i].x, &aa[i].y, &aa[i].v); for (int i = 1; i <= m; i++) scanf ("%d%d%d", &bb[i].x, &bb[i].y, &bb[i].v); Init (); memset (allrow, 0, sizeof (allrow)); memset (allcol, 0, sizeof (allcol)); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { scanf ("%d", &cap[i][j]); allrow[i] += cap[i][j]; allcol[j] += cap[i][j]; AddEdge (Dis (i, j), i, n + j, INF - cap[i][j], cap[i][j]); } for (int i = 1; i <= n; i++) AddEdge (0, 0, i, aa[i].v - allrow[i], allrow[i]); for (int i = 1; i <= m; i++) AddEdge (0, n + i, n + m + 1, bb[i].v - allcol[i], allcol[i]); N = n + m + 2; SS (N - 1); } return 0; } |
- « 上一篇:poj2516
- poj2125:下一篇 »