Continue to be the smallest weight ring overlay.
Note that the edges are bidirectional.
#include <cstdlib> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm > #include <fstream> #include <iostream> #include <vector> #include <cctype> #define REP (i, L, R) for (int i=l; i<=r; i++) #define CLR (x, C) memset (x, C, sizeof (x)) #define N 1234#define M 23456#define MAX 1<<30 #define LL Long longusing namespace Std;int read () {int x=0, f=1; char Ch=getchar (); while (!isdigit (CH)) {if (ch== '-') f=- 1; Ch=getchar (); }while (IsDigit (ch)) {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} struct Edge{int x, y, z, N;} E[m]; int Fir[n], en;int N, M, L[n], st[n], lx[n], Ly[n];bool vx[n], vy[n];inline void Add (int x, int y, int z) {en++, e[en]= (EDG e) {x, y, Z, Fir[x]}, fir[x]=en;en++, e[en]= (edge) {y, x, Z, Fir[y]}, fir[y]=en;} BOOL Find (int x) {vx[x]=1;for (int o=fir[x], y=e[o].y; o; o=e[o].n, y=e[o].y) {if (Vy[y]) Continue;int a=lx[x]+ly[y]-e[o].z if (!a) {vy[y]=1; if (!l[y] | | Find (L[y])) {l[y]=x; return 1;}} else St[y]=min (St[y], a);} return false;} inline int km () {clr (ly, 0), CLR (l, 0); Rep (i, 1, N) lx[i]=-max;rep (i, 1, en) if (lx[e[i].x]<e[i].z) lx[e[i].x]=e[i].z;r EP (I, 1, N) if (Lx[i]==-max) return 1;rep (i, 1, N) {rep (j, 1, N) st[j]=max;while (1) {CLR (VX, 0); CLR (VY, 0); if (Find (i)) b Reak; int A=max;rep (j, 1, N) if (!vy[j] && st[j]<a) a=st[j]; if (A==max) Break;rep (j, 1, N) if (Vx[j]) Lx[j]-=a;rep (j, 1, N) if (vy[j]) ly[j]+=a; else St[j]-=a;}} int A=0;rep (i, 1, N) if (!l[i]) return 1; else A+=lx[i]+ly[i];return A;} int main () {int t=read (), tt=0; while (tt++<t) {n=read (), M=read (); en=0; CLR (fir, 0); Rep (I, 1, m) {int x=read (), Y=read (), Z=read (); ADD (x, y,-Z); }int ans=-km (); if (ans!=-1) printf ("Case%d:%d\n", tt, ANS); else printf ("Case%d:no\n", TT);} return 0;}
HDU-3435 A New Graph Game