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;
    }
    --------------------------------------------------------------------------------
  • 相关阅读:
    策略梯度训练cartpole小游戏
    关于不执行整个大项目而是执行其中一部分独立文件夹的时候的python运行方法
    和textrank4ZH代码一模一样的算法详细解读
    K8s常用命令
    python标准库
    chrome通过devtools来查看Devtools Extension与浏览器内核实际通信的数据情况
    修改文件MD5值
    使用charles过滤网络请求
    git中working tree, index, commit
    Maven中settings.xml的配置项说明
  • 原文地址:https://www.cnblogs.com/20003238wzc--/p/4852845.html
Copyright © 2011-2022 走看看