国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 互联网 > ZOJ 1553 Evacuation Plan

ZOJ 1553 Evacuation Plan

来源:程序员人生   发布时间:2014-10-02 08:00:00 阅读次数:1703次


最小费用最大流.....

建图: 

源点 到 每栋楼  连容量为B,花费为0 的边

每个避难所 到 汇点  连容量为C,花费为0 的边

楼 到 避难所 连容量INF,花费 曼哈顿距离+1 的边


跑费用流后比较.... POJ 2175时限只有一秒.....会超时


Evacuation Plan
Time Limit: 10000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

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 file 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.

Process to the end of file.


Output

If The City Council's plan is optimal, then write to the output file 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
3 4
-3 3 5
-2 -2 6
2 2 5
-1 1 3
1 1 4
-2 -2 7
0 -1 3
3 0 1 1
0 0 6 0
0 4 0 1


Sample Output

SUBOPTIMAL
3 0 1 1
0 0 6 0
0 4 0 1
OPTIMAL

Source

Northeastern Europe 2002

[Submit]   [Go Back]   [Status]  



#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn=1000; const int INF=0x3f3f3f3f; struct Edge { int to,next,cap,flow,cost; }edge[maxn*maxn]; int Adj[maxn],Size,N; void init() { Size=0; memset(Adj,-1,sizeof(Adj)); } void addedge(int u,int v,int cap,int cost) { edge[Size].to=v; edge[Size].next=Adj[u]; edge[Size].cost=cost; edge[Size].cap=cap; edge[Size].flow=0; Adj[u]=Size++; } int Add_Edge(int u,int v,int cap,int cost) { addedge(u,v,cap,cost); int tk=Size-1; addedge(v,u,0,-cost); return tk; } int dist[maxn],vis[maxn],pre[maxn]; bool spfa(int s,int t) { queue<int> q; for(int i=0;i<N;i++) { dist[i]=INF; vis[i]=false; pre[i]=-1; } dist[s]=0; vis[s]=true; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap>edge[i].flow&& dist[v]>dist[u]+edge[i].cost) { dist[v]=dist[u]+edge[i].cost; pre[v]=i; if(!vis[v]) { vis[v]=true; q.push(v); } } } } if(pre[t]==-1) return false; return true; } int MinCostMaxFlow(int s,int t,int &cost) { int flow=0; cost=0; while(spfa(s,t)) { int Min=INF; for(int i=pre[t];~i;i=pre[edge[i^1].to]) { if(Min>edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow; } for(int i=pre[t];~i;i=pre[edge[i^1].to]) { edge[i].flow+=Min; edge[i^1].flow-=Min; cost+=edge[i].cost*Min; } flow+=Min; } return flow; } int n,m; struct PT { int x,y,c; }Build[maxn],She[maxn]; int Dist[maxn][maxn],mp[maxn][maxn],liu[maxn][maxn]; int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); for(int i=1;i<=n;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); Build[i]=(PT){a,b,c}; } for(int i=1;i<=m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); She[i]=(PT){a,b,c}; } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { Dist[i][j]=abs(Build[i].x-She[j].x)+abs(Build[i].y-She[j].y)+1; int t=Add_Edge(i,n+j,INF,Dist[i][j]); mp[i][j]=t; } } for(int i=1;i<=n;i++) { Add_Edge(0,i,Build[i].c,0); } for(int i=1;i<=m;i++) { Add_Edge(i+n,n+m+1,She[i].c,0); } N=n+m+2; int flow,cost; flow=MinCostMaxFlow(0,n+m+1,cost); int C=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%d",&liu[i][j]); C+=liu[i][j]*Dist[i][j]; } } if(C==cost) { puts("OPTIMAL"); } else { puts("SUBOPTIMAL"); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { printf("%d%c",edge[mp[i][j]].flow,(j!=m)?' ':' '); } } } } return 0; }



生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生