HDU 4888 Redraw Beautiful Drawings(网络流 + 判环(不能用强联通))
来源:互联网 发布:985软件工程硕士工资 编辑:程序博客网 时间:2024/05/18 03:13
题意和这个差不多http://blog.csdn.net/llzhh/article/details/77353532,只不过这道题改成棋盘上每个点的大小是0到k的。
建图什么的都一样,但是我发现我用一开始强联通的写法是过不去的,因为判环的地方tarjan是找极大强联通分量,这并不是我想要的,比如样例中一行四列那个。但是在上一题竟然过了。而且上一题没有判断行和列的和都相等也过了,数据实在是太水了。。。
这个判环我自己写了一个然后发现一直wa,到了下午突然发现,这都是源于多加了一个break。。。去掉就过了。。。一天又过去了。。。
判环我的方法是,dfs一下,每次进去都是在vst里面跑,如果发现某个点的vst已经标记,那么就返回1.否则我们在遍历完这个点后将他的标记擦去。这样还是怕超时,所以在外面再加上一个bvst,用于全局,如果这个点已经进去过就不再进去。。。具体还是看代码吧。。。
代码如下:
#include<bits/stdc++.h>using namespace std;const int maxn = 1005;const int maxm = maxn * maxn;const int INF = 0x3f3f3f3f;int n, m, k;int head[maxn],cur[maxn],nx[maxm<<1],to[maxm<<1],flow[maxm<<1],ppp=0;struct Dinic{int dis[maxn];int s, t;int ans;void init() {memset(head, -1, sizeof(head));ppp = 0;}void AddEdge(int u, int v, int c){to[ppp]=v;flow[ppp]=c;nx[ppp]=head[u];head[u]=ppp++;swap(u,v);to[ppp]=v;flow[ppp]=0;nx[ppp]=head[u];head[u]=ppp++;}bool BFS(){memset(dis, -1, sizeof(dis));dis[s] = 1; queue<int> Q;Q.push(s);while(!Q.empty()){int x = Q.front();Q.pop();for(int i = head[x]; ~i; i = nx[i]){if(flow[i] && dis[to[i]] == -1){dis[to[i]] = dis[x] + 1;Q.push(to[i]);}}}return dis[t] != -1;}int DFS(int x, int maxflow){if(x == t || !maxflow){ans += maxflow;return maxflow;}int ret = 0, f;for(int &i = cur[x]; ~i; i = nx[i]){if(dis[to[i]] == dis[x] + 1 && (f = DFS(to[i], min(maxflow, flow[i])))){ret += f;flow[i] -= f;flow[i^1] += f;maxflow -= f;if(!maxflow)break;}}return ret;}int solve(int source, int tank){s = source;t = tank;ans = 0;while(BFS()){memcpy(cur, head, sizeof(cur));DFS(s, INF);}return ans;}}dinic;bool vst[maxn], bvst[maxn];bool dfs(int u, int pre) {//cout << u << ' ' << pre << '\n';for(int i = head[u]; ~i; i = nx[i]) {int v = to[i];if(v == pre || flow[i] == 0)continue;if(vst[v]) {return 1;}vst[v] = 1;bvst[v] = 1;if(dfs(v, u)) {vst[v] = 0;return 1;}vst[v] = 0;}return 0;}int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endifwhile(scanf("%d%d%d", &n, &m, &k) != EOF) {int sum1 = 0, sum2 = 0;dinic.init();int s = n + m, t = s + 1;for(int i = 0, tmp; i < n; i++) {scanf("%d", &tmp);dinic.AddEdge(s, i, tmp);sum1 += tmp;}for(int i = 0, tmp; i < m; i++) {scanf("%d", &tmp);dinic.AddEdge(n + i, t, tmp);sum2 += tmp;}int start = ppp;for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {dinic.AddEdge(i, n + j, k);}}int ans = dinic.solve(s, t);if(ans != sum1 || sum1 != sum2) {printf("Impossible\n");continue;}bool ok = 0;memset(bvst, 0, sizeof(bvst));for(int i = 0; i < n; i++) {if(bvst[i])continue;//memset(vst, 0, sizeof(vst));bvst[i] = 1;vst[i] = 1;if(dfs(i, i)) {ok = 1;}vst[i] = 0;}if(ok)printf("Not Unique\n");else {printf("Unique\n");for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {printf("%d%c", k - flow[start], j + 1 == m ? '\n' : ' ');start += 2;}}}} return 0;}
阅读全文
0 0
- HDU 4888 Redraw Beautiful Drawings(网络流 + 判环(不能用强联通))
- HDUOJ--4888--Redraw Beautiful Drawings【isap】网络流+判环
- hdu 4888 Redraw Beautiful Drawings(最大流,判环)
- Hdu-4888 Redraw Beautiful Drawings 网络流
- HDU 4888 Redraw Beautiful Drawings 网络流(矩阵模型)
- HDU 4888 Redraw Beautiful Drawings 网络流 建图
- hdu 4888 Redraw Beautiful Drawings 2014多校三补题 网络流
- hdu 4888 Redraw Beautiful Drawings 网络流+搜索
- 【网络流】 HDOJ 4888 Redraw Beautiful Drawings
- hdu 4888 Redraw Beautiful Drawings(最大流)
- hdu 4888 Redraw Beautiful Drawings 最大流
- hdu 4888 Redraw Beautiful Drawings(最大流)
- HDU 4888 Redraw Beautiful Drawings(最大流)
- hdu 4888 Redraw Beautiful Drawings (最大流)
- hdu - 4888 - Redraw Beautiful Drawings(最大流)
- hdu 4888 Redraw Beautiful Drawings
- HDU-4888-Redraw Beautiful Drawings
- hdu 4888 Redraw Beautiful Drawings
- js实现动图效果
- Java-基本注解入门(Annotation)
- opencv获取编码fourcc
- 超过指定高度出现滚动条
- java +Spring MVC +MyBatis 实现Excle 导出
- HDU 4888 Redraw Beautiful Drawings(网络流 + 判环(不能用强联通))
- su和sudo的简单笔记
- null == undefined ?
- MongoDB索引探究
- 【机器学习入门】Andrew NG《Machine Learning》课程笔记之四:分类、逻辑回归和过拟合
- 线程安全
- 概率统计——基础运算法则
- msyql 全量备份脚本
- 513. Find Bottom Left Tree Value