hdu 1853 | hdu 3488 | hdu3435

제목:http://acm.hdu.edu.cn/showproblem.php?pid=1853
이분 도의 최 우선 일치.최소 비용 흐름 을 구하 다.모든 점 이 일치 하 는 최소 비용 을 요구 하고 KM 알고리즘 을 직접 사용 하면 됩 니 다.
다음은 1853 AC 코드 입 니 다.
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 305;
const int INF = (1<<30)-1;
int g[maxn][maxn];
int lx[maxn],ly[maxn];
int match[maxn];
bool visx[maxn],visy[maxn];
int slack[maxn];
int n;
bool dfs(int cur){
     visx[cur] = true;
     for(int y=1;y<=n;y++){
         if(visy[y])   continue;
         int t=lx[cur]+ly[y]-g[cur][y];
         if(t==0){
            visy[y] = true;
            if(match[y]==-1||dfs(match[y])){
                match[y] = cur;
                return true;
            }
         }
         else if(slack[y]>t){
                 slack[y]=t;
         }
     }
     return false;
}
int KM(){
    memset(match,-1,sizeof(match));
    memset(ly,0,sizeof(ly));
    for(int i=1 ;i<=n;i++){
         lx[i]=-INF;
       for(int j=1;j<=n;j++)
           if(g[i][j]>lx[i])   lx[i]=g[i][j];
   }
   for(int x=1;x<=n;x++){
        for(int i=1;i<=n;i++)  slack[i]=INF;
        while(true){
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(dfs(x))  break;
            int d=INF;
            for(int i=1;i<=n;i++){
               if(!visy[i]&&d>slack[i])     d=slack[i];
            }
            for(int i=1;i<=n;i++){
               if(visx[i])                  lx[i]-=d;
            }
            for(int i=1;i<=n;i++){
               if(visy[i])                 ly[i]+=d;
               else                        slack[i]-=d;
            }
        }
   }
    int result = 0;
    for(int i = 1; i <=n; i++){
        if(match[i]==-1||g[match[i]][i]==-INF)
            return 1;
      if(match[i]>-1)
        result += g[match[i]][i];
    }
    return result;
}
int main(){
    int m;
    int a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(g,0,sizeof(g));
        for(int i=1;i<=n;i++)
         for(int j=1;j<=n;j++)
             g[i][j]=-INF;
        for(int i=0;i<m;i++) {
            scanf("%d%d%d",&a,&b,&c);
            if(g[a][b]<-c)
            g[a][b]=-c;
        }
           int ans=-KM();
           printf("%d
",ans); } return 0; }

다음은 3488 AC 코드 입 니 다.
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 205;
const int INF = (1<<30)-1;
int g[maxn][maxn];
int lx[maxn],ly[maxn];
int match[maxn];
bool visx[maxn],visy[maxn];
int slack[maxn];
int n;
bool dfs(int cur){
     visx[cur] = true;
     for(int y=1;y<=n;y++){
         if(visy[y])   continue;
         int t=lx[cur]+ly[y]-g[cur][y];
         if(t==0){
            visy[y] = true;
            if(match[y]==-1||dfs(match[y])){
                match[y] = cur;
                return true;
            }
         }
         else if(slack[y]>t){
                 slack[y]=t;
         }
     }
     return false;
}
int KM(){
    memset(match,-1,sizeof(match));
    memset(ly,0,sizeof(ly));
    for(int i=1 ;i<=n;i++){
         lx[i]=-INF;
       for(int j=1;j<=n;j++)
           if(g[i][j]>lx[i])   lx[i]=g[i][j];
   }
   for(int x=1;x<=n;x++){
        for(int i=1;i<=n;i++)  slack[i]=INF;
        while(true){
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(dfs(x))  break;
            int d=INF;
            for(int i=1;i<=n;i++){
               if(!visy[i]&&d>slack[i])     d=slack[i];
            }
            for(int i=1;i<=n;i++){
               if(visx[i])                  lx[i]-=d;
            }
            for(int i=1;i<=n;i++){
               if(visy[i])                 ly[i]+=d;
               else                        slack[i]-=d;
            }
        }
   }
    int result = 0;
    for(int i = 1; i <=n; i++){
        if(match[i]==-1||g[match[i]][i]==-INF)
            return 1;
      if(match[i]>-1)
        result += g[match[i]][i];
    }
    return result;
}
int main(){
    int m,T;
    int a,b,c;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        memset(g,0,sizeof(g));
        for(int i=1;i<=n;i++)
         for(int j=1;j<=n;j++)
             g[i][j]=-INF;
        for(int i=0;i<m;i++) {
            scanf("%d%d%d",&a,&b,&c);
            if(g[a][b]<-c)
            g[a][b]=-c;
        }
           int ans=-KM();
           printf("%d
",ans); } return 0; }

다음은 3435 AC 코드 입 니 다.
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1005;
const int INF = (1<<30)-1;
int g[maxn][maxn];
int lx[maxn],ly[maxn];
int match[maxn];
bool visx[maxn],visy[maxn];
int slack[maxn];
int n;
bool dfs(int cur){
     visx[cur] = true;
     for(int y=1;y<=n;y++){
         if(visy[y])   continue;
         int t=lx[cur]+ly[y]-g[cur][y];
         if(t==0){
            visy[y] = true;
            if(match[y]==-1||dfs(match[y])){
                match[y] = cur;
                return true;
            }
         }
         else if(slack[y]>t){
                 slack[y]=t;
         }
     }
     return false;
}
int KM(){
    memset(match,-1,sizeof(match));
    memset(ly,0,sizeof(ly));
    for(int i=1 ;i<=n;i++){
         lx[i]=-INF;
       for(int j=1;j<=n;j++)
           if(g[i][j]>lx[i])   lx[i]=g[i][j];
   }
   for(int x=1;x<=n;x++){
        for(int i=1;i<=n;i++)  slack[i]=INF;
        while(true){
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(dfs(x))  break;
            int d=INF;
            for(int i=1;i<=n;i++){
               if(!visy[i]&&d>slack[i])     d=slack[i];
            }
            for(int i=1;i<=n;i++){
               if(visx[i])                  lx[i]-=d;
            }
            for(int i=1;i<=n;i++){
               if(visy[i])                 ly[i]+=d;
               else                        slack[i]-=d;
            }
        }
   }
    int result = 0;
    for(int i = 1; i <=n; i++){
        if(match[i]==-1||g[match[i]][i]==-INF)
            return 1;
      if(match[i]>-1)
        result += g[match[i]][i];
    }
    return result;
}
int main(){
    int m,T,ca=1;
    int a,b,c;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        memset(g,0,sizeof(g));
        for(int i=1;i<=n;i++)
         for(int j=1;j<=n;j++)
             g[i][j]=-INF;
        for(int i=0;i<m;i++) {
            scanf("%d%d%d",&a,&b,&c);
            if(g[a][b]<-c)
            g[a][b]=-c;
            if(g[b][a]<-c)
            g[b][a]=-c;
        }
           int ans=-KM();
        if(ans==-1)
           printf("Case %d: NO
",ca++); else printf("Case %d: %d
",ca++,ans); } return 0; }

좋은 웹페이지 즐겨찾기