# Hdoj--2426--interesting Housing problem (maximum charge flow)

Source: Internet
Author: User

Interesting Housing problemTime limit:10000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2972 Accepted Submission (s): 1068

Problem Descriptionfor Any school, it's hard-to-find a feasible accommodation plan with every student assigned to a Suita BLE apartment While keeping everyone happy, let alone an optimal one. Recently the president of University ABC, Peterson, is facing a similar problem. While Peterson does isn't like the idea of delegating the task directly to the class advisors as so many other schools is D Oing, he still wants to design a creative plan such that no student was assigned to a hostel he/she dislikes, and the overall Quality of the plan should be maximized. Nevertheless, Peterson does not know how the this task could is accomplished, so he asks do solve this so-called "interest ing "problem for him.
Suppose that there is N students and M rooms. Each student are asked to some rooms, not necessarily all M rooms, by stating how he/she likes the guest. The rating can represented as an integer, positive value meaning that the student consider the the the The Good Quali Ty, zero indicating neutral, or negative implying that the student does isn't like living in the. Note that you can never assign a student to a hostel which he/she have not rated, as the absence of rating indicates Student cannot live in the same as other reasons.
With limited information available, you ' ve decided to simply find an assignment such that every student are assigned to a R Oom he/she has rated, No. Students was assigned to the same, and the sum of rating was maximized while satisfying P Eterson ' s requirement. The question is ... what's exactly is the answer?

Inputthere is multiple test cases in the input file. Each test case begins with three integers, N, M, and E (1 <= n <=, 0 <= M <=, 0 <= E <= min (n * M, 50000)), followed by E-lines, each line containing three numbers, Si, Ri, Vi, (0 <= Si < N, 0 <= Ri < M, | vi| <= 10000), describing the rating Vi given by student Sifor. It is guaranteed, so each student would rate each of the once.
Each case was followed by one blank line. Input ends with End-of-file.

Outputfor Each test case, "Please output one integer, the requested value," on a "line", or-1 if no solution could be Found. Use the format as indicated in the sample output.

Sample Input
`3 5 50 1 50 2 71 1 61 2 32 4 51 1 10 0 01 1 0`

Sample Output
`Case 1:18case 2:0case 3:-1`

Source2008 Asia Hangzhou Regional Contest Online
Test instructions: N person, M room, everyone has a favorite and do not like the room, they will remain neutral to some rooms, A,b,c said that the first person to the B room attitude, c>0 expression like, equals zero means neutral, less than 0 is not like, Ask the maximum evaluation value after allocation can be how many very simple maximum cost flow, super-source Point Super Meeting point to people also have room to build edge, edge right is 1, cost is, c>=0 when a to C build side, Edge right is 1, cost C
`#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std;# Define MAXN 5555#define maxm 501000#define INF 0x3f3f3f3fint pre[maxn],vis[maxn],head[maxn],n,m,e,dis[maxn],cnt;int Source,sink;struct node{int U,v,cap,flow,cost,next;} Edge[maxm];void init () {memset (head,-1,sizeof (head)); cnt=0;}      void Add (int a,int b,int c,int d) {node E={a,b,c,0,d,head[a]};      Edge[cnt]=e;      head[a]=cnt++;      Node E1={b,a,0,0,-d,head[b]};      Edge[cnt]=e1;  head[b]=cnt++;      } bool BFS (int s,int t) {memset (vis,0,sizeof (VIS));      memset (dis,-inf,sizeof (dis));      memset (pre,-1,sizeof (pre));      Vis[s]=1;      dis[s]=0;      queue<int>q;      Q.push (s);          while (!q.empty ()) {int U=q.front ();          Q.pop ();          vis[u]=0;              for (int i=head[u];i!=-1;i=edge[i].next) {node e=edge[i];                if (Dis[e.v]<dis[u]+e.cost&&e.cap>e.flow) {  Dis[e.v]=dis[u]+e.cost;                  Pre[e.v]=i;                      if (!vis[e.v]) {Q.push (E.V);                  Vis[e.v]=1;  }}}} return pre[t]!=-1;      } void MCMF (int s,int t,int &cost,int &flow) {flow=cost=0;          while (BFS (s,t)) {int min=inf;              for (int i=pre[t];i!=-1;i=pre[edge[i^1].v]) {node e=edge[i];          Min=min (Min,e.cap-e.flow);              } for (int i=pre[t];i!=-1;i=pre[edge[i^1].v]) {edge[i].flow+=min;              Edge[i^1].flow-=min;          Cost+=edge[i].cost*min;      } flow+=min; }} void Getmap () {int a,b,c;for (int i=0;i<e;i++) {scanf ("%d%d%d", &a,&b,&c), if (c>=0) Add (a+1,b+1+n,1 , c);} for (int i=1;i<=n;i++) Add (source,i,1,0), for (int i=1;i<=m;i++) Add (i+n,sink,1,0);} int main () {int k=1;while (scanf ("%d%d%d", &n,&m,&e)!=eof) {init (); Source=0,sink=n+m+1;getmap (); int COST,FLOW;MCMF (Source,sink,cost,flow);p rintf ("Case%d:", k++); if (flow==n) printf ("%d\n", cost); elseprintf (" -1\n");} return 0;}`

Hdoj--2426--interesting Housing problem (maximum charge flow)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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