hdu1853 km algorithm

Source: Internet
Author: User

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

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.