多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > ZOJ 1553 Evacuation Plan

ZOJ 1553 Evacuation Plan

來源:程序員人生   發布時間:2014-10-02 08:00:00 閱讀次數:1829次


最小費用最大流.....

建圖: 

源點 到 每棟樓  連容量為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; }



生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美曰逼 | 日本中文字幕在线观看视频 | 日韩精品欧美一区二区三区 | 欧美成人亚洲高清在线观看 | 欧美乱大交xxxxx在线观看 | 精品影视网站入口 | 亚洲成av人影片在线观看 | 久久国内 | 欧美一区二区三区四区五区六区 | 亚洲小视频网站 | 欧美一级日韩在线观看 | 黄色淫片| hh99me福利毛片在线看 | 亚洲天堂在线视频观看 | 亚洲一区在线播放 | 好爽好大www视频在线播放 | 国产一区曰韩二区欧美三区 | 日本网站免费 | 亚洲三级图片 | 性欧美高清极品xx | xxxx网| 亚洲精品国产字幕久久不卡 | 久久久久在线 | 美国毛片在线观看 | 在线爱爱 | 成年人的天堂 | 最近的中文字幕免费完整 | 亚洲第一免费 | 欧美人与动性行为另类 | xxx日本护士 | 久久精品国产6699国产精 | xxxxx在线视频 | 伊人高清 | 欧洲美女人牲交一级毛片 | 99精品福利 | 国产区成人综合色在线 | 一级黄色欧美 | 精品久久久久久中文字幕女 | 伊人色在线观看 | 天堂一码二码专区 | 黄色网址中文字幕 |