1 //hdu18532#include <stdio.h>3#include <string.h>4 #defineINF 999999995 intmap[103][103],pr[103],pl[103],visr[103],visl[103],slack[103],match[103];6 intn,m;7 intDfsintu)8 {9 intI,j,val;Ten Onevisl[u]=1; A for(i=1; i<=n;i++) - { - if(!Visr[i]) the { -val=pr[i]+pl[u]-Map[u][i]; - if(val==0) - { +visr[i]=1; - if(match[i]==-1||DFS (Match[i])) + { Amatch[i]=u; at return 1; - } - } - if(val>0&&slack[i]>val) -slack[i]=Val; - } in } - return 0; to } + voidkm () - { the inti,j,res=0, D; * for(i=0; i<=n;i++) $pl[i]=INF;Panax Notoginsengmemset (PR,0,sizeof(PR)); -memset (match,-1,sizeof(Match)); the for(i=1; i<=n;i++) + { A for(j=1; j<=n;j++) theslack[j]=INF; + while(1) - { $memset (VISR,0,sizeof(VISR)); $memset (VISL,0,sizeof(VISL)); - if(Dfs (i)) - Break; theD=INF; - for(j=1; j<=n;j++)Wuyi { the if(!visr[j]&&d>Slack[j]) -D=Slack[j]; Wu } - for(j=1; j<=n;j++) About { $ if(Visr[j]) -pr[j]+=D; - if(Visl[j]) -pl[j]-=D; A } + } the } - } $ intMain () the { the inti,j; the while(SCANF ("%d%d", &n,&m)! =EOF) the { - for(i=0; i<=n;i++) in for(j=0; j<=n;j++) themap[i][j]=-INF; the About for(i=0; i<m;i++) the { the intx, y, z thescanf"%d%d%d",&x,&y,&z); + if(map[x][y]<-z) - { themap[x][y]=-Z;Bayi } the } the /*For (i=1;i<=n;i++) - { - For (j=1;j<=n;j++) the printf ("%d", map[i][j]); the printf ("\ n"); the }*/ the km (); - intflag=1; the intres=0; the for(i=1; i<=n;i++) the {94 if(map[match[i]][i]==-INF) the{flag=0; Break;} the Elseres+=Map[match[i]][i]; the }98 if(!flag) printf ("-1\n"); About Else -printf"%d\n",-res);101 }102}
hdu1853 km algorithm