祝贺生活回到了正确的道路上了。。。裸的KM
---------------------------------------------------------------------------------------------------------------------------------------
Time Limit: 2000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
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?
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.
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\n");
F=false;
break;
}
else ans+=f[ll[i]][i];
}
}
if(F)
printf("%d\n",ans);
}
return 0;
}
#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\n");
F=false;
break;
}
else ans+=f[ll[i]][i];
}
}
if(F)
printf("%d\n",ans);
}
return 0;
}
--------------------------------------------------------------------------------
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步