(消一個負圈) poj 2175 Evacuation Plan

來源:互聯網
上載者:User
Evacuation Plan
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 968   Accepted: 236   Special Judge

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 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 62 2 5-1 1 31 1 4-2 -2 70 -1 33 1 1 00 0 6 00 3 0 2

Sample Output

SUBOPTIMAL3 0 1 10 0 6 00 4 0 1/*題目大意:檢驗市長的方案是不是最優的,如果是則輸出OPTIMAL,否則輸出SUBOPTIMAL和更優的方案.題目解答:將其轉化成圖模型,求當前的方案是不是最小費用最大流。驗證是不是最小費用就是去尋找有沒有可能路徑長度變小:用bellman方法去尋找有沒有一個負圈使得路徑縮短而不影響最大流.(即消一個負圈)ps:本題十分詭異,之前提交WA,後來檢查多變沒改,提交過了。Source CodeProblem: 2175  User: wawadimu Memory: 560K  Time: 282MS Language: C++  Result: Accepted Source Code */#include<iostream>#include<algorithm>#include<cmath>#include<queue>using namespace std;#define inf INT_MAX#define maxn 220struct  Node{int i,j;//座標int cap;}X[maxn/2],Y[maxn/2];int dis[maxn][maxn];//a[t]源點到節點t的當前最大流量int ans[maxn][maxn];//儲存市長提出的方案int p[maxn];//記錄路徑前一個節點int num[maxn],d[maxn];bool vis[maxn];int n,m;//房屋數和避難所數 void fun(int S,int T) { int i,j,k; int u,v; memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); queue<int> q; q.push(S); vis[S]=1; for(i=0;i<T;i++) d[i]=inf; d[S]=0;//d[x]源點S到x的當前最短距離 bool flag=true; while(!q.empty() && flag)//尋找一個負圈 { u=q.front();q.pop();vis[u]=false;for(v=0;v<T;v++){if(dis[u][v]!=inf && d[v] > d[u] + dis[u][v])//鬆弛{d[v]=d[u]+dis[u][v];p[v]=u;//記錄路徑if(!vis[v])//沒有在隊列{vis[v]=true;q.push(v);num[v]++;}}if(num[v] >T) {flag=false; break;}//大於節點次數,證明有負環} } if(flag)printf("OPTIMAL/n"); else { memset(vis,0,sizeof(vis)); while(!vis[v])//注意跳出點不一定是負圈的點 { //cout<<v<<"  "; vis[v]=true; v=p[v];  } u=v;while(p[u]!=v)//數組作為值做運算出錯(值會莫名其妙改變),所以用臨時變數儲存 { k=p[u]; //cout<<u<<endl; if( u< k) ans[k-m][u]++;//人->房 else ans[ u-m ][ k ]--;//房->人 u=k; } if( u < v) ans[v-m][u]++;//人->房 else ans[u-m][v]--;//房->人 printf("SUBOPTIMAL/n");//列印注意格式問題 for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { printf("%d",ans[i][j]); if(j!=m) printf(" "); } printf("/n"); } } return ; }int main(){//freopen("2175.txt","r",stdin);int i,j;while(scanf("%d%d",&n,&m)!=EOF){for(i=1;i<=n;i++){scanf("%d%d%d", &X[i].i, &X[i].j, &X[i].cap);//printf("%d %d %d/n", X[i].i, X[i].j, X[i].cap);}for(i=1;i<=m;i++){scanf("%d%d%d", &Y[i].i, &Y[i].j, &Y[i].cap);//printf("%d %d %d/n",Y[i].i, Y[i].j, Y[i].cap);}memset(ans,0,sizeof(ans));for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf("%d",&ans[i][j]);//printf("%d ",ans[i][j]);ans[j][0]+=ans[i][j];}}int S=0,T=n+m+1;for(i=0;i<T;i++)for(j=0;j<T;j++)dis[i][j]=inf;for(i=1;i<=m;i++){if(ans[i][0]!=0) dis[S][i]=0;//if(Y[i].cap-ans[i][0]) dis[i][S]=0;//有容納空間}for(i=1;i<=n;i++){for(j=1;j<=m;j++){dis[i+m][j]=abs(X[i].i - Y[j].i) + abs(X[i].j - Y[j].j) + 1;//房->人 正距離if(ans[i][j]>0) dis[j][i+m]=-dis[i+m][j]; //人->房 負距離}}fun(S,T);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.