Topic Links:
http://www.lydsy.com/JudgeOnline/problem.php?id=4500
Exercises
From the row to the Lie Jian side, representing a lattice a[i][j], all operations on each vertex can be combined together with SUM[XI],
Then the topic is equivalent to asking for SUM[XI]+SUM[XJ]==A[XI][XJ];
Equivalent to: sum[xj]-(-sum[xi]) ==A[XI][XJ]
Equivalent to: Sum[xj]-sum ' [XI]<=A[XI][XJ] && sum[xj]-sum ' [XI]>=A[XI][XJ]
Equivalent to: Sum[xj]<=sum ' [xi]+w && sum ' [xi]<=sum[xj]+ (-W)
All can be done with a differential constraint. Run the shortest way, and you can have a negative ring.
#include <iostream>#include<cstring>#include<cstdio>#include<vector>#include<queue>using namespacestd;Const intMAXN =2222;Const intINF =0x3f3f3f3f;structEdge {intV, W; Edge (intVintW): V (v), W (w) {} Edge () {}};intN, M,k;vector<Edge>Egs;vector<int>G[MAXN];voidAddedge (intUintVintW) {G[u].push_back (Egs.size ()); Egs.push_back (Edge (V,w));}BOOLINQ[MAXN];intCNT[MAXN];intD[MAXN];BOOLSPFA () {memset (INQ,0,sizeof(INQ)); memset (CNT,0,sizeof(CNT)); for(inti =0; I <= n + m; i++) D[i] =INF; Queue<int>Q; d[0] =0; inq[0] =true; Q.push (0); while(!Q.empty ()) { intU =Q.front (); Q.pop (); Inq[u]=false; for(inti =0; I < g[u].size (); i++) {Edge& e =Egs[g[u][i]]; if(D[E.V] > D[u] +e.w) {D[E.V]= D[u] +E.W; if(!INQ[E.V]) {Q.push (E.V); INQ[E.V]=true; if(++CNT[E.V] > n+m+1) { return false; } } } } } return true;}voidinit () { for(inti =0; I <= n + m; i++) g[i].clear (); Egs.clear ();}intMain () {intTC; scanf ("%d", &TC); while(tc--) {scanf ("%d%d%d", &n, &m, &k); Init (); for(inti =0; I < K; i++) { intu, V, W; scanf ("%d%d%d", &u, &v, &W); Addedge (U, v+N, W); Addedge (v+n, U,-W); } for(inti =1; I <= n + m; i++) {Addedge (0I0); } if(SPFA ()) {puts ("Yes"); } Else{puts ("No"); } } return 0;}/*2 in 1, 2, 1 2, 2, 1, 2, 1, 2 1*/
Bzoj 4500: Matrix difference constraint