标签:des style blog http io color os ar java
3 4 -3 3 5 -2 -2 6 2 2 5 -1 1 3 1 1 4 -2 -2 7 0 -1 3 3 1 1 0 0 0 6 0 0 3 0 2
SUBOPTIMAL 3 0 1 1 0 0 6 0 0 4 0 1
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 500; 18 struct arc{ 19 int to,flow,cost,next; 20 arc(int x = 0,int y = 0,int z = 0,int nxt = -1){ 21 to = x; 22 flow = y; 23 cost = z; 24 next = nxt; 25 } 26 }; 27 arc e[maxn*maxn]; 28 int head[maxn],d[maxn],x[maxn],y[maxn],c[maxn]; 29 int tot,S,T,n,m,sf[maxn],bf[maxn],p[maxn],cnt[maxn]; 30 bool in[maxn]; 31 void add(int u,int v,int f1,int f2,int co){ 32 e[tot] = arc(v,f1,co,head[u]); 33 head[u] = tot++; 34 e[tot] = arc(u,f2,-co,head[v]); 35 head[v] = tot++; 36 } 37 int getDis(int i,int j){ 38 return abs(x[i] - x[j]) + abs(y[i] - y[j]) + 1; 39 } 40 int spfa(){ 41 for(int i = S; i <= T; ++i){ 42 d[i] = INF; 43 cnt[i] = 0; 44 in[i] = false; 45 p[i] = -1; 46 } 47 queue<int>q; 48 d[T] = 0; 49 q.push(T); 50 while(!q.empty()){ 51 int u = q.front(); 52 q.pop(); 53 in[u] = false; 54 for(int i = head[u]; ~i; i = e[i].next){ 55 if(e[i].flow && d[e[i].to] > d[u] + e[i].cost){ 56 d[e[i].to] = d[u] + e[i].cost; 57 p[e[i].to] = i; 58 if(!in[e[i].to]){ 59 in[e[i].to] = true; 60 if(++cnt[e[i].to] > T) return e[i].to; 61 q.push(e[i].to); 62 } 63 } 64 } 65 } 66 return -1; 67 } 68 int main(){ 69 while(~scanf("%d %d",&n,&m)){ 70 memset(head,-1,sizeof(head)); 71 memset(sf,0,sizeof(sf)); 72 memset(bf,0,sizeof(bf)); 73 T = n + m + 1; 74 S = tot = 0; 75 for(int i = 1; i <= n + m; ++i) 76 scanf("%d %d %d",x+i,y+i,c+i); 77 for(int i = 1; i <= n; ++i){ 78 for(int j = 1; j <= m; ++j){ 79 int tmp = 0; 80 scanf("%d",&tmp); 81 add(i,j+n,INF - tmp,tmp,getDis(i,j+n)); 82 sf[j] += tmp; 83 bf[i] += tmp; 84 } 85 } 86 for(int i = 1; i <= n; ++i) add(S,i,c[i] - bf[i],bf[i],0); 87 for(int i = 1; i <= m; ++i) add(i+n,T,c[i+n] - sf[i],sf[i],0); 88 int u,v,o = spfa(); 89 if(o == -1) puts("OPTIMAL"); 90 else{ 91 memset(in,false,sizeof(in)); 92 while(!in[o]){ 93 in[o] = true; 94 o = e[p[o]^1].to; 95 } 96 v = u = o; 97 do{ 98 u = e[p[v]^1].to; 99 e[p[v]].flow--; 100 e[p[v]^1].flow++; 101 v = u; 102 103 }while(u != o); 104 puts("SUBOPTIMAL"); 105 for(int i = 1; i <= n; ++i){ 106 for(int j = 1; j <= m; ++j){ 107 printf("%d%c",e[(i-1)*m*2 + 2*(j-1)+1].flow,j == m?‘\n‘:‘ ‘); 108 } 109 } 110 } 111 } 112 return 0; 113 }
标签:des style blog http io color os ar java
原文地址:http://www.cnblogs.com/crackpotisback/p/4064523.html