意甲冠军:要在N好M行和列以及列的数字矩阵和,每个元件的尺寸不超过9,询问是否有这样的矩阵,是独一无二的N(1 ≤ N ≤ 500) , M(1 ≤ M ≤ 500)。

主题链接:http://acm.hdu.edu.cn/showproblem.php?

pid=4975

——>>方法如:http://blog.csdn.net/scnu_jiechao/article/details/40658221

先做hdu - 4888,再来做此题的时候,感觉这题好 SB 呀。将代码提交后自己就 SB 了。咋 TLE 了??

此题卡时间比較严。。能够在判环的地方优化一下4888的代码。由于原选建好的图中的行到列的边,在判环的时候,每条边会被判 (N - 1) * (M - 1) + 1 次,假设先对残量网络又一次建图,那么就仅仅会在建图的时候被判一次。差距甚远。

注:输入开挂会RE。猜想输入数据中有负数。。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm> using std::min;
using std::queue; const int MAXN = 500 * 2 + 10;
const int MAXM = 500 * 500 + 2 * MAXN;
const int INF = 0x3f3f3f3f; struct EDGE
{
int from;
int to;
int cap;
int flow;
int nxt;
} edge[MAXM << 1]; int N, M, kase;
int sum;
int S, T;
int hed[MAXN], ecnt;
int cur[MAXN], h[MAXN];
bool impossible, bUnique; void Init()
{
impossible = false;
bUnique = true;
ecnt = 0;
memset(hed, -1, sizeof(hed));
} void AddEdge(int u, int v, int cap)
{
edge[ecnt].from = u;
edge[ecnt].to = v;
edge[ecnt].cap = cap;
edge[ecnt].flow = 0;
edge[ecnt].nxt = hed[u];
hed[u] = ecnt++;
edge[ecnt].from = v;
edge[ecnt].to = u;
edge[ecnt].cap = 0;
edge[ecnt].flow = 0;
edge[ecnt].nxt = hed[v];
hed[v] = ecnt++;
} bool Bfs()
{
memset(h, -1, sizeof(h));
queue<int> qu;
qu.push(S);
h[S] = 0;
while (!qu.empty())
{
int u = qu.front();
qu.pop();
for (int e = hed[u]; e != -1; e = edge[e].nxt)
{
int v = edge[e].to;
if (h[v] == -1 && edge[e].cap > edge[e].flow)
{
h[v] = h[u] + 1;
qu.push(v);
}
}
} return h[T] != -1;
} int Dfs(int u, int cap)
{
if (u == T || cap == 0) return cap; int flow = 0, subFlow;
for (int e = cur[u]; e != -1; e = edge[e].nxt)
{
cur[u] = e;
int v = edge[e].to;
if (h[v] == h[u] + 1 && (subFlow = Dfs(v, min(cap, edge[e].cap - edge[e].flow))) > 0)
{
flow += subFlow;
edge[e].flow += subFlow;
edge[e ^ 1].flow -= subFlow;
cap -= subFlow;
if (cap == 0) break;
}
} return flow;
} int Dinic()
{
int maxFlow = 0; while (Bfs())
{
memcpy(cur, hed, sizeof(hed));
maxFlow += Dfs(S, INF);
} return maxFlow;
} int ReadInt()
{
int ret = 0;
char ch; while ((ch = getchar()) && ch >= '0' && ch <= '9')
{
ret = ret * 10 + ch - '0';
} return ret;
} void Read()
{
int r, c;
int rsum = 0, csum = 0; scanf("%d%d", &N, &M);
S = 0;
T = N + M + 1;
getchar();
for (int i = 1; i <= N; ++i)
{
// r = ReadInt();
scanf("%d", &r);
rsum += r;
AddEdge(S, i, r);
}
for (int i = 1; i <= M; ++i)
{
// c = ReadInt();
scanf("%d", &c);
csum += c;
AddEdge(i + N, T, c);
} if (rsum != csum)
{
impossible = true;
return;
} sum = rsum;
for (int i = 1; i <= N; ++i)
{
for (int j = M; j >= 1; --j)
{
AddEdge(i, j + N, 9);
}
}
} void CheckPossible()
{
if (impossible) return;
if (Dinic() != sum)
{
impossible = true;
}
} void AddEdge(int u, int v)
{
edge[ecnt].to = v;
edge[ecnt].nxt = hed[u];
hed[u] = ecnt++;
} void ReBuild()
{
memset(hed, -1, sizeof(hed));
int total = ecnt;
ecnt = 0;
for (int e = 0; e < total; ++e)
{
if (edge[e].cap > edge[e].flow)
{
AddEdge(edge[e].from, edge[e].to);
}
}
} bool vis[MAXN];
bool CheckCircle(int x, int f)
{
vis[x] = true;
for (int e = hed[x]; e != -1; e = edge[e].nxt)
{
int v = edge[e].to;
if (v != f)
{
if (vis[v]) return true;
if (CheckCircle(v, x)) return true;
}
}
vis[x] = false;
return false;
} void CheckUnique()
{
if (impossible) return;
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= N; ++i)
{
if (CheckCircle(i, -1))
{
bUnique = false;
return;
}
}
} void Output()
{
printf("Case #%d: ", ++kase);
if (impossible)
{
puts("So naive!");
}
else if (bUnique)
{
puts("So simple!");
}
else
{
puts("So young!");
}
} int main()
{
int T; scanf("%d", &T);
while (T--)
{
Init();
Read();
CheckPossible();
ReBuild();
CheckUnique();
Output();
} return 0;
}

hdu - 4975 - A simple Gaussian elimination problem.(最大流量)的更多相关文章

  1. HDU 4975 A simple Gaussian elimination problem.

    A simple Gaussian elimination problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be ...

  2. hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...

  3. hdu 4975 A simple Gaussian elimination problem 最大流+找环

    原题链接 http://acm.hdu.edu.cn/showproblem.php?pid=4975 这是一道很裸的最大流,将每个点(i,j)看作是从Ri向Cj的一条容量为9的边,从源点除法连接每个 ...

  4. HDOJ 4975 A simple Gaussian elimination problem.

    和HDOJ4888是一样的问题,最大流推断多解 1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉...... 2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開 ...

  5. hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)

    这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...

  6. A simple Gaussian elimination problem.(hdu4975)网络流+最大流

    A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...

  7. A simple Gaussian elimination problem.

    hdu4975:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:给你一个n*m的矩阵,矩阵中的元素都是0--9,现在给你这个矩阵的每一行和每一列的和 ...

  8. hdu4975 A simple Gaussian elimination problem.(最大流+判环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:和hdu4888基本一样( http://www.cnblogs.com/a-clown/ ...

  9. hdu 4972 A simple dynamic programming problem(高效)

    pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...

随机推荐

  1. [Android学习笔记]Fragment使用

    一.android.app.Fragment 与 android.support.v4.app.Fragment 区别 support.v4.app.Fragment是为了给低版本Android使用的 ...

  2. Swift - 协议(protocol)

    1,Swift中协议类似于别的语言里的接口,协议里只做方法的声明,包括方法名.返回值.参数等信息,而没有具体的方法实现. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  3. ThinkPhp学习02

    原文:ThinkPhp学习02 一.什么是MVC                M -Model 编写model类 对数据进行操作 V -View  编写html文件,页面呈现 C -Controll ...

  4. [IDEs]Eclipse For Mac , 常用快捷键

    Cmd + O:  查看.java中得方法,变量,等结构 Cmd + T:     查看继承关系 Cmd + K:          查找下一个选中的成员 Cmd + E:  查看已经打开的文件 Cm ...

  5. Eclipse 修改maven 仓储Repository位置

    简述: 使用两个Nexus, 需要配置两份不同的Maven仓库 步骤: 1. 下载新的Maven运行包 2. 进入conf/ 修改setting.xml项 <localRepository> ...

  6. Python语法

  7. UVA 10003 Cutting Sticks

    题意:在给出的n个结点处切断木棍,并且在切断木棍时木棍有多长就花费多长的代价,将所有结点切断,并且使代价最小. 思路:设DP[i][j]为,从i,j点切开的木材,完成切割需要的cost,显然对于所有D ...

  8. 模仿《百度音乐HD》添加到下载框动画

    上次听有人说喜欢<百度音乐HD>添加到下载动画 ,我就尝试模仿了下,没想到,今天code4app(地址)也有了这个,但是 这个动画基本相同,我们的思路还是部一样的. 都可以参考 .主要关键 ...

  9. js动态添加Div

    利用JavaScript动态添加Div的方式有很多,在这次开发中有用到,就搜集了一下比较常用的. 一.在一个Div前添加Div <html> <body> <div id ...

  10. PYQT4 + Python2.6 + eric4-4.2.2a的安装全过程

    PYQT4 + Python2.6 + eric4-4.2.2a的安装全过程 - beike - ITeye技术网站 PYQT4 + Python2.6 + eric4-4.2.2a的安装全过程 博客 ...