Recently one known microprocessor productioner has developed the new type of microprocessors that can be used in difficult mathematical calculations. The processor contains N so called nodes that are connected by M channels. Data organized in packets, pass from source node to target node by channels and are processed by the intermediate nodes.
Each node has its level that determines the type of work this node does. The source node has level 1 while the target node has level L. For data to be correctly processed each packet of it must pass in order all nodes with levels from 1 to L - that is, first it must be processed by the source node, after that by some node of level 2, so on, and finally by the target node.
Nodes can process as much data as they are asked to, however channels can only transmit the limited amount of data in a unit of time. For synchronization reasons, any data can only be transmitted from a node with level i to some node with level i + 1 and cannot be transmitted between nodes which levels differ by more than one or from a node of higher level to a node of lower level. Nodes are so fast that they can process data packet immediately, so as soon as it reaches the node it is ready to be transmitted to the node of the next level.
No data should stall in any node and no node can produce its own data, so each unit of time the number of packets coming to any node except source and target, must be equal to the number of packets leaving this node.
The scheme of data transmission that satisfies the conditions provided is called the data flow. Data flow is called blocking if there is no way to increase the value of the data flow just increasing the amount of data passing by some channels (however, there may be the way to increase it, decreasing the amount of data for some channels and increasing for other ones).
The first line of the input file contains three integer numbers - N, M and L (2 <= N <= 1 500, 1 <= M <= 300 000, 2 <= L <= N). Let nodes be numbered from 1 to N. The second line contains N integer numbers, i-th of them is the level li of the i-th node (1 <= li <= L). Only one node has level 1, that is the source node, and only one node has level L - that is the target node.
Next M lines describe channels, each lines contains three integer numbers a, b and c - nodes connected by this channel and its capacity in packets per unit of time (1 <= a, b <= N, lb = la+1, 1 <= c <= 106).
Two nodes can be connected by at most one channel.
6 7 4 1 2 3 4 3 2 1 2 3 2 3 3 3 4 4 1 6 4 6 3 2 5 4 3 6 5 4
3 3 4 4 1 3 3
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 = 1510; 18 struct arc{ 19 int to,flow,next; 20 arc(int x = 0,int y = 0,int z = -1){ 21 to = x; 22 flow = y; 23 next = z; 24 } 25 }; 26 arc e[600010]; 27 int head[maxn],d[maxn],lev[maxn],_rank[maxn],in[maxn],out[maxn]; 28 int n,m,L,S,T,hd,tl,tot,cur[maxn],q[maxn]; 29 void myscanf(int &x){ 30 char ch; 31 while((ch = getchar()) > '9' || ch < '0'); 32 x = 0; 33 x = x*10 + ch - '0'; 34 while((ch = getchar()) >= '0' && ch <= '9') 35 x = x*10 + ch - '0'; 36 } 37 void add(int u,int v,int flow){ 38 e[tot] = arc(v,flow,head[u]); 39 head[u] = tot++; 40 e[tot] = arc(u,0,head[v]); 41 head[v] = tot++; 42 } 43 bool cmp(const int &x,const int &y){ 44 return lev[x] < lev[y]; 45 } 46 void greedy(){ 47 memset(in,0,sizeof(in)); 48 memset(out,0,sizeof(out)); 49 sort(_rank+1,_rank+n+1,cmp); 50 in[S] = INF; 51 for(int i = 1; i <= n; i++){ 52 int u = _rank[i]; 53 for(int j = head[u]; ~j; j = e[j].next){ 54 if(!(j&1) && in[u] > out[u]){ 55 int f = min(e[j].flow,in[u] - out[u]); 56 in[e[j].to] += f; 57 out[u] += f; 58 } 59 } 60 } 61 memset(in,0,sizeof(in)); 62 in[T] = INF; 63 for(int i = n; i >= 1; --i){ 64 int v = _rank[i]; 65 for(int j = head[v]; ~j; j = e[j].next){ 66 int u = e[j].to; 67 if(j&1 && out[u] > in[u]){ 68 int f = min(e[j^1].flow,min(out[u] - in[u],in[v])); 69 in[v] -= f; 70 in[u] += f; 71 e[j].flow += f; 72 e[j^1].flow -= f; 73 } 74 } 75 } 76 } 77 bool bfs(){ 78 memset(d,-1,sizeof(d)); 79 hd = tl = 0; 80 q[tl++] = S; 81 d[S] = 1; 82 while(hd < tl){ 83 int u = q[hd++]; 84 for(int i = head[u]; ~i; i = e[i].next){ 85 if(e[i].flow && d[e[i].to] == -1){ 86 d[e[i].to] = d[u] + 1; 87 q[tl++] = e[i].to; 88 } 89 } 90 } 91 return d[T] > -1; 92 } 93 int dfs(int u,int low){ 94 if(u == T) return low; 95 int tmp = 0,a; 96 for(int &i = cur[u]; ~i; i = e[i].next){ 97 if(e[i].flow && d[e[i].to] == d[u] + 1 && (a=dfs(e[i].to,min(low,e[i].flow)))){ 98 tmp += a; 99 low -= a; 100 e[i].flow -= a; 101 e[i^1].flow += a; 102 if(!low) break; 103 } 104 } 105 if(!tmp) d[u] = -1; 106 return tmp; 107 } 108 int dinic(){ 109 int tmp = 0; 110 while(bfs()){ 111 memcpy(cur,head,sizeof(head)); 112 tmp += dfs(S,INF); 113 } 114 return tmp; 115 } 116 int main() { 117 int i,u,v,cap; 118 scanf("%d %d %d",&n,&m,&L); 119 memset(head,-1,sizeof(head)); 120 for(i = 1; i <= n; i++){ 121 myscanf(lev[i]); 122 _rank[i] = i; 123 if(lev[i] == 1) S = i; 124 else if(lev[i] == L) T = i; 125 } 126 for(int i = tot = 0; i < m; i++){ 127 myscanf(u); 128 myscanf(v); 129 myscanf(cap); 130 add(u,v,cap); 131 } 132 greedy(); 133 dinic(); 134 for(int i = 0; i < m; i++) 135 printf("%d\n",e[i<<1|1].flow); 136 return 0; 137 }