题目链接

给出n个城市, 以及初始时每个城市的人数以及目标人数。初始时有些城市是相连的。 每个城市的人只可以待在自己的城市或走到与他相邻的城市, 相邻, 相当于只能走一条路。

如果目标状态不可达, 输出no, 否则输出每个城市的人都是怎么走的, 比如第一个城市有2个人走到了第二个城市, 1个人留在了第一个城市, 那么输出的第一行前两个数就是1, 2。

很明显的网络流, 输出那里写了好久...

首先判断能否可达, 如果初始状态的人数与目标状态不一样, 一定不可达, 其次, 如果建完图跑网络流的结果与所有目标城市人数的和不一样, 也不可达。

建图的话, 将一个城市拆成两个点, (u, u') 中间连边inf, 说明可以随便走, 源点和u相连, 权值为初始状态人数, u'与汇点相连, 人数为目标状态人数。 如果两个城市之间有路相连, 那么加边(u, v'), (v, u'), 权值为inf。

每个城市的人是怎么走的, 应该看反向边的流量, 如果边(v', u)的权值为x, 那么ans[u][v] = x。

具体看代码。

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5;
int q[maxn*], head[maxn*], dis[maxn/], s, t, num, vis[], val[][];
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*];
void init() {
num = ;
mem1(head);
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], ); head[v] = num++;
}
int bfs() {
mem(dis);
dis[s] = ;
int st = , ed = ;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
if(u == t) {
return limit;
}
int cost = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
int main()
{
int n, m, x, y, sum = , tmp = ;
cin>>n>>m;
init();
s = , t = n*+;
for(int i = ; i<=n; i++) {
scanf("%d", &x);
add(s, i, x);
tmp += x;
}
for(int i = ; i<=n; i++) {
scanf("%d", &x);
add(i+n, t, x);
sum += x;
add(i, i+n, inf);
}
while(m--) {
scanf("%d%d", &x, &y);
add(x, y+n, inf);
add(y, x+n, inf);
}
int ans = dinic();
if(ans != sum||sum!=tmp) {
cout<<"NO"<<endl;
return ;
}
cout<<"YES"<<endl;
for(int u = ; u<=n; u++) {
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v>n) {
val[u][v-n] = e[i^].c; //反向边流量
}
}
}
for(int i = ; i<=n; i++) {
for(int j = ; j<=n; j++) {
cout<<val[i][j]<<" ";
}
cout<<endl;
}
return ;
}

最新文章

  1. 【干货分享】流程DEMO-请休假
  2. Java Socket
  3. HTML相关
  4. 一步一步学WebSocket(二) 使用SuperWebSocket实现自己的服务端
  5. netcat命令
  6. 数据库IO简介
  7. hdu2639(背包求第k优解)
  8. [LeetCode] Boundary of Binary Tree 二叉树的边界
  9. hash与encrypt
  10. R语言的精度和时间效率比较(简单版)
  11. Hadoop生态圈-构建企业级平台安全方案
  12. URLConnection和HttpURLConnection
  13. Kubelet bootstrap 流程
  14. DJango 基础(6)
  15. 利用order by 进行盲注
  16. python基础-动态加载lazy_import(利用__import__)
  17. 找到SVN版本机上项目的地址
  18. Call to undefined function mysqli_connect() in xx.连接数据库出现mysqli_connect()未定义的问题。
  19. IIS7.5 配置应用程序初始化功能
  20. caffe Python API 之Accuracy

热门文章

  1. MSSQL查询连接数
  2. MySQL常用指令
  3. 查询EBS在线用户SQL(R12)
  4. IOS app启动过程
  5. Java核心技术卷1Chapter7笔记 图形程序设计
  6. 浅谈C中的指针和数组(五)
  7. CFILE追加写入文件
  8. Linux中的那些英文缩写和她的含义们
  9. [LeetCode]题解(python):146-LRU Cache
  10. 在VHDL中,&ldquo;传输延迟&rdquo;和&ldquo;惯性延迟&rdquo;