HDU3488 tour--binary graph maximum weight matching km algorithm

Source: Internet
Author: User

Title Link: https://vjudge.net/problem/HDU-3488

Tour

Time limit:3000/1000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 3720 Accepted Submission (s): 1777


Problem DescriptionIn The Kingdom of Henryy, there is n (2 <= N <=) cities, with M (M <= 30000) one-way road s connecting them. You is lucky enough to has a chance to has a tour in the kingdom. The route should is designed as:the route should contain one or more loops. (a loop is a route like:a->b->......->p->a.)
Every city should is just in one route.
A Loop should has at least and cities. In one route, each city should is visited just once. (The only exception is, the first and the last city should are the same and this are visited twice.)
The total distance the N roads you has chosen should be minimized.

Inputan integer T in the first line indicates the number of the the test cases.
In each test case, the first line contains integers N and M, indicating the number of the cities and the one-way roads . Then M lines followed, each of the line have three integers U, V and W (0 < W <= 10000), indicating that there is a road fro M U to V, with the distance of W.
It is guaranteed, at least one valid arrangement of the tour is existed.
A Blank line was followed after each test case.

Outputfor each test case, output a line with exactly one integer, which are the minimum total distance.

Sample Input16 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 4

Sample Output42

Source2010 acm-icpc multi-university Training Contest (6)--host by BIT

Recommendzhouzeyong

Exercises

The code is as follows:

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Const intINF =0x3f3f3f3f;5 ConstLL LNF =9e18;6 Const intMoD = 1e9+7;7 Const intMAXN = 2e2+Ten;8 9 intNX, NY;Ten intG[MAXN][MAXN]; One intLINKER[MAXN], LX[MAXN], LY[MAXN]; A intSLACK[MAXN]; - BOOLVISX[MAXN], VISY[MAXN]; -  the BOOLDFS (intx) - { -VISX[X] =true; -      for(inty =1; y<=ny; y++) +     { -         if(Visy[y])Continue; +         intTMP = Lx[x] + ly[y]-G[x][y]; A         if(tmp==0) at         { -Visy[y] =true; -             if(linker[y]==-1||DFS (Linker[y])) -             { -Linker[y] =x; -                 return true; in             } -         } to         Else +Slack[y] =min (slack[y], TMP); -     } the     return false; * } $ Panax Notoginseng intKM () - { theMEMSET (linker,-1,sizeof(linker)); +memset (Ly,0,sizeof(ly)); A      for(inti =1; i<=nx; i++) the     { +Lx[i] =-INF; -          for(intj =1; j<=ny; J + +) $Lx[i] =Max (Lx[i], g[i][j]); $     } -  -      for(intx =1; x<=nx; X + +) the     { -          for(inti =1; i<=ny; i++)WuyiSlack[i] =INF; the          while(true) -         { Wumemset (VISX,0,sizeof(VISX)); -memset (Visy,0,sizeof(Visy)); About  $             if(DFS (x)) Break; -             intD =INF; -              for(inti =1; i<=ny; i++) -                 if(!Visy[i]) AD =min (d, slack[i]); +  the              for(inti =1; i<=nx; i++) -                 if(Visx[i]) $Lx[i]-=D; the              for(inti =1; i<=ny; i++) the             { the                 if(Visy[i]) ly[i] + =D; the                 ElseSlack[i]-=D; -             } in         } the     } the  About     intres =0; the      for(inti =1; i<=ny; i++) the         if(linker[i]!=-1) theRes + =G[linker[i]][i]; +     returnRes; - } the Bayi intMain () the { the     intT, N, M; -scanf"%d", &T); -      while(t--) the     { thescanf"%d%d", &n,&m); theNX = NY =N; theMemset (G,0,sizeof(g)); -          for(inti =1; i<=nx; i++) the              for(intj =1; j<=ny; J + +) theG[I][J] =-INF; the          for(inti =1; i<=m; i++)94         { the             intu, V, W; thescanf"%d%d%d", &u, &v, &W); theG[U][V] = max (G[u][v],-W);98         } About  -printf"%d\n", -KM ());101     }102}
View Code

HDU3488 tour--binary graph maximum weight matching km algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.