Bzoj 2324 Rescue Pikachu

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=2324

Idea: Minimum cost maximum flow

Consider setting the array d[k][i][j], which represents the shortest path to only the first K cities, I to J

And then you can build the diagram.

S->0 flow rate is K, the cost is 0

I->i+n traffic is INF with a cost of 0

I+n->t Flow rate is 1

I+n->j traffic is INF, cost is D[J][I][J]

S->i flow rate is 1, the cost is 0 represents if a person to the I city, it can continue to walk, thereby renewing a new traffic.

1#include <algorithm>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <iostream>6 #defineINF 0x7fffffff7 intd[205][205][205],a[205][205],k;8 inttot,go[200005],next[200005],first[200005],cost[200005],flow[200005];9 intop[200005],dis[200005],c[200005],vis[200005],edge[200005], from[200005];Ten intS,t,n,m,ans; One intRead () { A     CharCh=getchar ();intt=0, f=1; -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} the     returnt*F; - } - voidInsertintXintYintZintl) { -tot++; +go[tot]=y; -next[tot]=First[x]; +first[x]=tot; Aflow[tot]=Z; atcost[tot]=l; - } - voidAddintXintYintZintl) { -Insert (x,y,z,l); op[tot]=tot+1; -Insert (Y,x,0,-L); op[tot]=tot-1; - } in BOOLSPFA () { -      for(intI=0; i<=t;i++) todis[i]=0x3f3f3f3f, vis[i]=0; +     intH=1, t=1; c[1]=s;vis[s]=1;d is[s]=0; -      while(h<=t) { the         intnow=c[h++]; *          for(intI=first[now];i;i=Next[i]) { $             intPur=Go[i];Panax Notoginseng             if(dis[pur]>dis[now]+cost[i]&&Flow[i]) { -dis[pur]=dis[now]+Cost[i]; theedge[pur]=i; +                  from[pur]=Now ; A                 if(Vis[pur])Continue; thevis[pur]=1; +c[++t]=pur; -             } $         } $vis[now]=0; -     } -     returndis[t]!=0x3f3f3f3f; the } - voidUpdata () {Wuyi     intmn=0x7ffffff; the      for(intI=t;i!=s;i= from[i]) { -mn=std::min (Mn,flow[edge[i]); Wu     } -      for(intI=t;i!=s;i= from[i]) { Aboutans+=mn*Cost[edge[i]]; $flow[edge[i]]-=mn; -flow[op[edge[i]]]+=mn; -     } - } A intMain () { +N=read (); M=read (); k=read (); the      for(intI=0; i<=n;i++) -       for(intj=0; j<=n;j++) $       if(i!=j) { thea[i][j]=0x3f3f3f3f; the}Else thea[i][j]=0; the      for(intI=1; i<=m;i++){ -         intU=read (), V=read (), w=read (); ina[u][v]=std::min (a[u][v],w); thea[v][u]=std::min (a[v][u],w); the     } About      for(intk=0; k<=n;k++){ the          for(intI=0; i<=n;i++) the           for(intj=0; j<=n;j++) theA[i][j]=std::min (a[i][k]+a[k][j],a[i][j]); +          for(intI=0; i<=n;i++) -           for(intj=0; j<=n;j++) thed[k][i][j]=A[i][j]; Bayi     } thes=2*n+2; t=s+1; theAdd (S,0K0); -      for(intI=0; i<=n;i++) -Add (i,i+n+1Inf0); the      for(intI=0; i<=n;i++) theAdd (I,t,1,0); the      for(intI=0; i<=n;i++) theAdd (s,i+n+1,1,0); -      for(intI=0; i<=n;i++) the       for(intj=i+1; j<=n;j++) the       if(d[j][i][j]<0x3f3f3f3f) theAdd (i+n+1, J,inf,d[j][i][j]);94      while(SPFA ()) Updata (); theprintf"%d\n", ans);  the}

Bzoj 2324 Rescue Pikachu

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.