Soldier and Traveling
阅读原文时间:2023年07月08日阅读:2

Time Limit: 1000ms

Memory Limit: 262144KB

64-bit integer IO format: %I64d      Java class name: (Any)

Submit Status

In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th city consists of _a__i_soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by atmoving along at most one road.

Check if is it possible that after roaming there will be exactly b__i soldiers in the i-th city.

Input

First line of input consists of two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 200).

Next line contains n integers a_1, _a_2, …, _a__n (0 ≤ a__i ≤ 100).

Next line contains n integers b_1, _b_2, …, _b__n (0 ≤ b__i ≤ 100).

Then m lines follow, each of them consists of two integers p and q (1 ≤ p, q ≤ np ≠ q) denoting that there is an undirected road between cities p and q.

It is guaranteed that there is at most one road between each pair of cities.

Output

If the conditions can not be met output single word "NO".

Otherwise output word "YES" and then n lines, each of them consisting of n integers. Number in the i-th line in the j-th column should denote how many soldiers should road from city i to city j (if i ≠ j) or how many soldiers should stay in city i (if i = j).

If there are several possible answers you may output any of them.

Sample Input

Input

4 4
1 2 6 3
3 5 3 1
1 2
2 3
3 4
4 2

Output

YES
1 0 0 0
2 0 0 0
0 5 1 0
0 0 2 1

Input

2 0
1 2
2 1

Output

NO
思路:最大流;
首先判断变前和变后的和是否相等,如果不等则直接输出NO,否则,转换为最大流求解,原点和原来的点连边权值为原来的人口,然后每个新的状态和汇点连边,权值为后来的人口,然后
按给的边连边,权值为原来的人口,然后跑最大流,判断最大流量是否为sum。最后的矩阵由反边得来,表示从上个点有人口转移而来,反边的值就是转移人口。

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 using namespace std;
10 typedef long long LL;
11 typedef struct pp
12 {
13 int to;
14 int cap;
15 int rev;
16 } aa;
17 vectorvec[205];
18 int level[205];
19 int iter[205];
20 int ans[105];
21 int bns[105];
22 void add(int from,int to,int cap);
23 void bfs(int s);
24 int dfs(int s,int t,int f);
25 int max_flow(int s,int t);
26 const int N = 1e9;
27 int ma[200][200];
28 int main(void)
29 {
30 int n,m;
31 scanf("%d %d",&n,&m);
32 int i,j;
33 int sum1 = 0;
34 int sum2 = 0;
35 for(i = 1; i <= n; i++) 36 { 37 scanf("%d",&ans[i]); 38 sum1 += ans[i]; 39 } 40 for(i = 1; i <= n; i++) 41 { 42 scanf("%d",&bns[i]); 43 sum2 += bns[i]; 44 } 45 if(sum1 != sum2) 46 printf("NO\n"); 47 else 48 { 49 for(i = 1;i <= n; i++) 50 { 51 add(0,i,ans[i]); 52 add(i+n,2*n+1,bns[i]); 53 add(i,i+n,ans[i]); 54 } 55 while(m--) 56 { 57 int x,y; 58 scanf("%d %d",&x,&y); 59 add(x,y+n,ans[x]); 60 add(y,x+n,ans[y]); 61 } 62 int ask = max_flow(0,2*n+1); 63 if(ask != sum1) 64 printf("NO\n"); 65 else 66 { 67 for(i = 1;i <= n;i++) 68 { 69 int x = i+n; 70 for(j = 0;j < vec[x].size(); j++) 71 { 72 aa no = vec[x][j]; 73 ma[no.to][i] = no.cap; 74 } 75 }printf("YES\n"); 76 for(i = 1;i <= n; i++) 77 { 78 for(j = 1;j <= n; j++) 79 { 80 if(j == 1) 81 printf("%d",ma[i][j]); 82 else printf(" %d",ma[i][j]); 83 } 84 printf("\n"); 85 } 86 } 87 }return 0; 88 } 89 void add(int from,int to,int cap) 90 { 91 pp nn; 92 nn.to = to; 93 nn.cap = cap; 94 nn.rev = vec[to].size(); 95 vec[from].push_back(nn); 96 nn.to = from; 97 nn.cap=0; 98 nn.rev = vec[from].size()-1; 99 vec[to].push_back(nn); 100 } 101 void bfs(int s) 102 { 103 queueque;
104 memset(level,-1,sizeof(level));
105 level[s]=0;
106 que.push(s);
107 while(!que.empty())
108 {
109 int v=que.front();
110 que.pop();
111 int i;
112 for(i=0; i0)
116 {
117 level[e.to]=level[v]+1;
118 que.push(e.to);
119 }
120 }
121 }
122 }
123 int dfs(int s,int t,int f)
124 {
125 if(s==t)
126 return f;
127 for(int &i=iter[s]; ilevel[s]&&e.cap>0)
131 {
132 int r=dfs(e.to,t,min(e.cap,f));
133 if(r>0)
134 {
135 e.cap-=r;
136 vec[e.to][e.rev].cap+=r;
137 return r;
138 }
139 }
140 }
141 return 0;
142 }
143 int max_flow(int s,int t)
144 {
145 int flow=0;
146 for(;;)
147 {
148 bfs(s);
149 if(level[t]<0)return flow; 150 memset(iter,0,sizeof(iter)); 151 int f; 152 while((f=dfs(s,t,N)) >0)
153 {
154 flow += f;
155 }
156 }
157 }

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章