There is N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy so, each cycle contain at least both cities, and each CI Ty belongs to one cycle exactly. Tom wants the total length of the "all" tours minimum, but he's too lazy to calculate. Can you help him?
Inputthere is several test cases in the input. You should process to the end of file (EOF).
The first line of all test case contains, integers N (n≤100) and M, indicating the number of cities and the number O F Roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there are A road from city A to city B, whose length is C. (1≤a,b≤n, A≠b, 1≤c≤1000).
Outputoutput one number for each test case, indicating the minimum length of the tours. If There is no such tours, output-1.
Sample Input
6 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 46 51 2 12 3 13 4 14 5 15 6 1
Sample Output
42-1
Hint
In the first sample, there was cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
Test instructions: Enter n vertex m edges to make these vertices ring, including at least two vertices. Outputs the smallest complete match and, if not present, the output-1.
Idea: A point in two, the formation of two sets, to find the smallest match, the weight of the negative after the maximum match, and finally return a negative value. If the graph consists of a plurality of forward rings, there must be a complete match, if the right value of the largest complete match exists, it must be the best complete match of the figure, if any of the matching edge is the initialized Benquan value, indicating that the complete match does not exist.
Before the water, and now write the headache, today only out of 3, their really good food ah, when can be as good as master, feel the algorithm really need to understand their own to deal with all kinds of deformation, the problem on the time-out
5 times, how do I know to judge the heavy side AH ~ ~ ~
#include <stdio.h>#include<string.h>#defineN 210#defineINF 0x3f3f3f3fintMap[n][n];intans,d;intW[n][n],lx[n],ly[n];intLinker[n],visx[n],visy[n];intN,m,nx,ny;intDfsintx) { inty,tmp; VISX[X]=1; for(y =1; Y <= ny; Y + +) { if(!Visy[y]) {tmp= Lx[x] + ly[y]-W[x][y]; if(!tmp) {Visy[y]=1; if(linker[y]==-1||DFS (Linker[y])) {Linker[y]=x; return 1; } } Else if(D >tmp) d=tmp; } } return 0;}intKM () {intx,y,i,j,sum; MEMSET (linker,-1,sizeof(linker)); memset (ly,0,sizeof(ly)); for(x =1; x <= NX; X + +) for(y =1, lx[x] =-inf; Y <= ny; Y + +) if(Lx[x] <W[x][y]) lx[x]=W[x][y]; for(x =1; x <= NX; X + +) { while(1) {D=INF; memset (VISX,0,sizeof(VISX)); memset (Visy,0,sizeof(Visy)); if(Dfs (x)) Break; for(i =1; I <= NX; i + +) if(Visx[i]) lx[i]-=D; for(i =1; I <= NY; i + +) if(Visy[i]) ly[i]+=D; }} sum=0; for(i =1; I <= NY; i + +) { if(W[linker[i]][i]! =-INF) Sum+=W[linker[i]][i]; Else return-1; } return-sum;}intMain () {intT1,t2,t3; while(SCANF ("%d%d", &n,&m)! =EOF) {NX= NY =N; for(inti =1; I <= N; i + +) for(intj =1; J <= N; J + +) W[i][j]= -INF; for(inti =1; I <= m; i + +) {scanf ("%d%d%d",&t1,&t2,&T3); if(-t3 > W[t1][t2])//repeat input to take small valueW[T1][T2] =-T3; } ans=KM (); printf ("%d\n", ans); } return 0;}
"Binary Map Matching primer 1" m-cyclic tour hdu1853 "Miles algorithm--judging self-weight side"