14更多学校的第二个问题

网络流量   分别以行,列作为结点建图

i行表示的结点到j列表示的结点的流量便是(i, j)的值

跑遍最大流   若满流了便是有解   推断是否unique  就是在残余网络中dfs。走能够添加流量的边,找到环即不唯一

dfs的时候一定要回溯!!

。。

#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <fstream>
using namespace std; //LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) //OTHER
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define all(x) (x).begin(),(x).end() //INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RS(s) scanf("%s", s) //OUTPUT
#define WI(n) printf("%d\n", n)
#define WS(n) printf("%s\n", n) //debug
//#define online_judge
#ifndef online_judge
#define dt(a) << (#a) << "=" << a << " "
#define debugI(a) cout dt(a) << endl
#define debugII(a, b) cout dt(a) dt(b) << endl
#define debugIII(a, b, c) cout dt(a) dt(b) dt(c) << endl
#define debugIV(a, b, c, d) cout dt(a) dt(b) dt(c) dt(d) << endl
#define debugV(a, b, c, d, e) cout dt(a) dt(b) dt(c) dt(d) dt(e) << endl
#else
#define debugI(v)
#define debugII(a, b)
#define debugIII(a, b, c)
#define debugIV(a, b, c, d)
#endif #define sqr(x) (x) * (x)
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const double eps = 1e-9;
const int MOD = 1000000007;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int maxn = 900; bool use[maxn];
struct Edge{
int from, to, cap, flow;
};
int MAX; struct Dinic{
int n, m ,s, t;
vector<Edge> edges;
VI G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn] ; void init(int nn)
{
this->n = nn;
REP(i, n) G[i].clear();
edges.clear();
} void addEdge(int from, int to, int cap)
{
edges.PB((Edge){from, to, cap, 0});
edges.PB((Edge){to, from, 0, 0});
m = edges.size();
G[from].PB(m - 2);
G[to].PB(m - 1);
} bool bfs()
{
CLR(vis, 0);
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
REP(i, G[x].size())
{
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow)
{
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
} int dfs(int x, int a)
{
if (x == t || a == 0) return a;
int flow = 0, f;
for (int& i = cur[x]; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0)
{
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
} int maxflow(int s, int t)
{
this-> s = s, this-> t = t;
int flow = 0;
while (bfs())
{
CLR(cur, 0);
flow += dfs(s, INF);
}
return flow;
} bool visit(int u, int fa)
{
if (u == 0 || u == MAX) return false;
use[u] = 1;
REP(i, G[u].size())
{
Edge& e = edges[G[u][i]];
// debugII(e.to, use[e.to]);
if (e.to != fa && e.cap > e.flow)
if (use[e.to] || visit(e.to, u))
return true;
}
use[u] = 0;
return false;
} }di; int main()
{
int n, m, k;
while (~RIII(n, m, k))
{
int x, sum1 = 0, sum2 = 0;
MAX = n + m + 1;
di.init(n + m + 2);
FE(i, 1, n)
{
RI(x);
sum1 += x;
di.addEdge(0, i, x);
}
FE(i, n + 1, n + m)
{
RI(x);
sum2 += x;
di.addEdge(i, n + m + 1, x);
}
FE(i, 1, n)
FE(j, n + 1, n + m)
di.addEdge(i, j, k);
if (sum2 != sum1)
{
puts("Impossible");
continue;
}
int ans = di.maxflow(0, n + m + 1);
if (ans < sum1)
{
puts("Impossible");
continue;
}
FE(i, 1, n)
{
CLR(use, 0);
if (di.visit(i, -1))
{
puts("Not Unique");
goto end;
}
}
puts("Unique");
for (int i = 2 * n + 2 * m; i < di.edges.size(); i += 2)
{
printf("%d", di.edges[i].flow);
if (i + 2 >= di.edges.size())
printf("\n");
else
printf(" ");
}
end:;
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

hdu4888 Redraw Beautiful Drawings的更多相关文章

  1. hdu4888 Redraw Beautiful Drawings 最大流+判环

    hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/6553 ...

  2. HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

    Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  3. HDU4888 Redraw Beautiful Drawings(最大流唯一性判定:残量网络删边判环)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4888 Description Alice and Bob are playing toget ...

  4. hdu4888 Redraw Beautiful Drawings(最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4888 题意:给一个矩阵没行的和和每列的和,问能否还原矩阵,如果可以还原解是否唯一,若唯一输出该矩阵. ...

  5. Redraw Beautiful Drawings(hdu4888)网络流+最大流

    Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...

  6. 【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】

    传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因 ...

  7. HDU Redraw Beautiful Drawings 推断最大流是否唯一解

    点击打开链接 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 ...

  8. HDU 4888 Redraw Beautiful Drawings(最大流+判最大流网络是否唯一)

    Problem Description Alice and Bob are playing together. Alice is crazy about art and she has visited ...

  9. HDU 4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

    题意:给定n*m个格子,每个格子能填0-k 的整数.然后给出每列之和和每行之和,问有没有解,有的话是不是唯一解,是唯一解输出方案. 思路:网络流,一共 n+m+2个点   源点 到行连流量为 所给的 ...

随机推荐

  1. ubuntu 搭建svn服务器

    1.安装Subversion sudo apt-get install subversion 2.创建资源库 cd /home/username/ svnserve -d -r /home/usern ...

  2. Maven安装中的问题

    按照<Maven实战>中的讲述,在安装完Maven后执行mvn -v的时候,出现了问题.在网上搜索到了解决办法: 引用:http://blog.csdn.net/xueyepiaoling ...

  3. 怎样让你的安卓手机瞬间变Firefox os 畅玩firefox os 应用

    Firefox os 手机迟迟不能在国内大面积上市.如今能买到的Firefox os手机国内就一款Firefox os ZET OPEN C ,但这款手机配置确实还不如人意.价格方面也不实惠,对于我们 ...

  4. WPF中不规则窗体与WebBrowser控件的兼容问题解决办法

    原文:WPF中不规则窗体与WebBrowser控件的兼容问题解决办法 引言 这几天受委托开发一个网络电视项目,要求初步先使用内嵌网页形式实现视频播放和选单,以后再考虑将网页中的所有功能整合进桌面程序. ...

  5. 在Button上、下、左、右位置加入图片和文字

    转载请注明出处:http://blog.csdn.net/droyon/article/details/37564419 非常多人有如标题所述的需求,并且大多数人採用了自己定义组件攻克了需求,事实上还 ...

  6. Delphi F11 全屏

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  7. 如何解决Windows8.1(32bit&amp;64bit)下Cisco VPN Client拨号时报442错误的问题

    Cisco VPN Cient大多数网络管理员.技术支持project最流行的教师和最终用户VPNclient一间.对于外部网络访问内部网络,技术类人员. 随着Windows8.1的推出.Cisco ...

  8. C#新DataColumn类Type生成的方法类型参数

    DataColumn有的需要等级Type构造类型的参数,如以下: // // 摘要: // 使用指定列名称和数据类型初始化 System.Data.DataColumn 类的新实例. // // 參数 ...

  9. oracle在schema是什么意思?

    看来有些人还在schema不明白的真正含义,今天,我再次整理.我希望能帮助. 我们先来看看它们的定义:A schema is a collection of database objects (use ...

  10. UVA 674 (入门DP, 14.07.09)

     Coin Change  Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We ...