zoukankan      html  css  js  c++  java
  • hdu2426: Interesting Housing Problem

    祝贺生活回到了正确的道路上了。。。裸的KM
    ---------------------------------------------------------------------------------------------------------------------------------------
    Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

     Status

    Description

    For any school, it is hard to find a feasible accommodation plan with every student assigned to a suitable 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 not like the idea of delegating the task directly to the class advisors as so many other schools are doing, he still wants to design a creative plan such that no student is assigned to a room he/she dislikes, and the overall quality of the plan should be maximized. Nevertheless, Peterson does not know how this task could be accomplished, so he asks you to solve this so-called "interesting" problem for him. 
    Suppose that there are N students and M rooms. Each student is asked to rate some rooms (not necessarily all M rooms) by stating how he/she likes the room. The rating can be represented as an integer, positive value meaning that the student consider the room to be of good quality, zero indicating neutral, or negative implying that the student does not like living in the room. Note that you can never assign a student to a room which he/she has not rated, as the absence of rating indicates that the student cannot live in the room for other reasons. 
    With limited information available, you've decided to simply find an assignment such that every student is assigned to a room he/she has rated, no two students are assigned to the same room, and the sum of rating is maximized while satisfying Peterson's requirement. The question is … what exactly is the answer? 
     

    Input

    There are multiple test cases in the input file. Each test case begins with three integers, N, M, and E (1 <= N <= 500, 0 <= M <= 500, 0 <= E <= min(N * M, 50000)), followed by E lines, each line containing three numbers, S i, R i, V i, (0 <= S i < N, 0 <= R i < M, |V i| <= 10000), describing the rating V igiven by student S i for room R i. It is guaranteed that each student will rate each room at most once. 
    Each case is followed by one blank line. Input ends with End-of-File. 
     

    Output

    For each test case, please output one integer, the requested value, on a single line, or -1 if no solution could be found. Use the format as indicated in the sample output. 
     

    Sample Input

    3 5 5 0 1 5 0 2 7 1 1 6 1 2 3 2 4 5 1 1 1 0 0 0 1 1 0
     

    Sample Output

    Case 1: 18 Case 2: 0 Case 3: -1
     

    Source

    2008 Asia Hangzhou Regional Contest Online
    a-------------------------------------------------------------------------------
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define rep(i,n) for(int i=1;i<=n;i++)
    #define clr(x,c) memset(x,c,sizeof(x))
    int read(){
     int x=0,f=1;
     char c=getchar();
     while(!isdigit(c)){
         if(c=='-')
           f=-1;
         c=getchar();
     }
     while(isdigit(c)){
      x=x*10+c-'0';
      c=getchar();
     }
     return x*f;
    }
    const int nmax=505;
    int lx[nmax],ly[nmax],s[nmax],t[nmax],f[nmax][nmax],ll[nmax],n,m,k;
    void init(){
     clr(f,-0x7f);
     clr(lx,0);
     clr(ly,0);
     clr(ll,0);
    }
    bool dfs(int i){
     s[i]=1;
     rep(j,m){
      if(lx[i]+ly[j]==f[i][j]&&!t[j]){
       t[j]=1;
       if(!ll[j]||dfs(ll[j])){
        ll[j]=i;
        return true;
       }
      }
     }
     return false;
    }
    void insert(){
     int inf=1<<30;
     rep(i,n)
       if(s[i])
         rep(j,m)
           if(!t[j])
             inf=min(inf,lx[i]+ly[j]-f[i][j]);
     rep(i,n)
       if(s[i])
         lx[i]-=inf;
     rep(i,m)
       if(t[i])
         ly[i]+=inf;
    }
    int main(){
     int cur=0;
     while(scanf("%d%d%d",&n,&m,&k)==3){
      cur++;
      init();
      rep(i,k){
       int u=read(),v=read(),o=read();
       if(o>=0)
         f[++u][++v]=o;
      }
      rep(i,n){
       rep(j,m){
        lx[i]=max(lx[i],f[i][j]);
       }
      }
      rep(i,n){
       while(1){
        clr(s,0);
        clr(t,0);
        if(dfs(i))
          break;
        else insert();
       }
      }
      bool F=true;
      int ans=0;
      printf("Case %d: ",cur);
      rep(i,m){
       if(ll[i]){
        if(f[ll[i]][i]<0){
         printf("-1 ");
         F=false;
         break;
        }
        else ans+=f[ll[i]][i];
       }
      }
      if(F)
         printf("%d ",ans);
     }
     return 0;
    }
    --------------------------------------------------------------------------------
  • 相关阅读:
    2016/4/7 省市县三级联动 下拉菜单式
    2016/4/5 Ajax ①用户名 密码 登陆 注册 ② 判断用户名是否已存在 ③点击按钮出现民族选项下拉菜单 ④DBDA类 加入Ajaxquery方法 数组变字符串 字符串拆分
    2016/4/2 json:js和jquery中轻量级数据交换格式 例: 窗口弹出 popwindow
    2016/4/1 jquery 与javascript关系 ①取元素 ②操作内容 ③操作属性 ④操作 样式 ⑤ 事件 点击变色
    2016/4/1 PDO:: 数据访问抽象层 ? :
    2016/3/31 ①全选时 下面选项全选中 ② 下面不选中时 全选取消 ③在“” 中 转义字符的使用 onclick="Checkpa(this,'flall')"; ④区别于分别实现 重点在于两种情况合并实现
    2016/3/30 租房子 ①建立租房子的增、删、改php页面 ②多条件查询 ③全选时 各部分全选中 任意checkbox不选中 全选checkbox不选中
    正则表达式
    HTML总结
    深入理解CSS盒子模型
  • 原文地址:https://www.cnblogs.com/20003238wzc--/p/4852845.html
Copyright © 2011-2022 走看看