• hdu 3488 Tour


    Tour

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 1393    Accepted Submission(s): 734


    Problem Description
    In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.)
    Every city should be just in one route.
    A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
    The total distance the N roads you have chosen should be minimized.
     
    Input
    An integer T in the first line indicates the number of the test cases.
    In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
    It is guaranteed that at least one valid arrangement of the tour is existed.
    A blank line is followed after each test case.
     
    Output
    For each test case, output a line with exactly one integer, which is the minimum total distance.
     
    Sample Input
    1 6 9 1 2 5 2 3 5 3 1 10 3 4 12 4 1 8 4 6 11 5 4 7 5 6 9 6 5 4
     
    Sample Output
    42
     第二个km算法的题,二分图就先到这吧
    粗浅的学习了下
    #include<stdio.h>
    #include<string.h>
    #define N 305
    #define INF 0x3f3f3f3f
    int map[N][N];
    int visx[N],visy[N],slack[N],linky[N];
    int lx[N],ly[N],n,m;
    int dfs(int x)
    {
         visx[x]=1;
         int i;
         for(i=1;i<=n;i++)
         {
              if(visy[i])
                   continue;
              int t=lx[x]-ly[i]-map[x][i];
              if(t==0)
              {
                   visy[i]=1;
                   if(linky[i]==-1||dfs(linky[i]))
                   {
                        linky[i]=x;
                        return 1;
                   }
              }
              else if(t<slack[i])
                   slack[i]=t;
         }
         return 0;
    }
    int km()
    {
         int i,j;
         memset(visy,0,sizeof(visy));
         for(i=1;i<=n;i++)
              for(j=1,lx[i]=-INF;j<=n;j++)
               if(map[i][j]>lx[i])
                   lx[i]=map[i][j];
               for(i=1;i<=n;i++)
               {
                    for(j=1;j<=n;j++)
                        slack[j]=INF;
                    while(1)
                    {
                         memset(visx,0,sizeof(visx));
                         memset(visy,0,sizeof(visy));
                         if(dfs(i))
                             break;
                         int d=INF;
                         for(j=1;j<=n;j++)
                         {
                              if(!visy[j]&&d>slack[j])
                                  d=slack[j];
                         }
                         for(j=1;j<=n;j++)
                             if(visx[j])
                             lx[j]-=d;
                         for(j=1;j<=n;j++)
                             if(visy[j])
                             ly[j]-=d;
                         else
                         slack[j]-=d;
    
                    }
               }
               int ans=0;
               for(i=1;i<=n;i++)
                   if(linky[i]>-1)
                   ans+=map[linky[i]][i];
               return ans;
    }
    int main()
    {
         int i,j,t,x,y,w;
         scanf("%d",&t);
         while(t--)
         {
              scanf("%d %d",&n,&m);
              for(i=1;i<=n;i++)
                   for(j=1;j<=n;j++)
                     map[i][j]=INF;
                     for(i=1;i<=m;i++)
                     {
                          scanf("%d %d %d",&x,&y,&w);
                          if(map[x][y]>w)
                             map[x][y]=w;
                     }
                     for(i=1;i<=n;i++)
                        for(j=1;j<=n;j++)
                        map[i][j]=-map[i][j];
              memset(linky,-1,sizeof(linky));
              printf("%d
    ",-km());
         }
         return 0;
    }
  • 相关阅读:
    百度云推送
    web请求报出 “超过了最大请求长度” 【注意:重启IIS】
    页面多个Jquery版本共存的冲突问题,解决方法!
    Web Api 中使用 PCM TO WAV 的语音操作
    Web Api 如何做上传文件的单元测试
    那些年收集的前端学习资源
    原创: 做一款属于自己风格的音乐播放器 (HTML5的Audio新特性)
    Web Api 接口文档制作
    如何在Asp.Net WebApi接口中,验证请求参数中是否携带token标识!
    JavaScript 面试题,给大家补补基础,加加油,埋埋坑!
  • 原文地址:https://www.cnblogs.com/llei1573/p/3273412.html
Copyright © 2020-2023  润新知