这是通化邀请赛的题,当时比赛的时候还完全没想法呢,看来这几个月的训练还是有效果的。。。

题意要求(1) |ai| < T for all i   (2) (vi, vj) in E <=> |ai - aj| >= T。由于(1)条件的存在,所以(2)条件能成立当且仅当ai和aj一正一负。由此可见,图中某条路上的元素正负值分别为正->负->正->负。。。显然当图中存在奇环的时候是无解的。判断奇环用二分染色,color[i]=0表示假设i节点未被染色,1表示假设i节点权值为正,2为负。

如果图中没有奇环呢?对于图中的一条边<u, v>,如果color[u]=1,那么显然a[u]-a[v] >= T,color[u]=2, 也就是 -(a[u]-a[v]) >= T;

而如果<u, v>不是图中的边, 必然有 | a[u]-a[v] |  < T。由color数组也同样能得到两个不等式。

得到不等式组后无脑跑spfa判负环就行了。。。光是判负环的spfa是不用考虑加入0节点的,在初始化得时候将每个节点加到队列一次就够了。而且d数组也完全可以初始化为0。因为你只需要判负环而已。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
#include<cstdio>
#include<bitset>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define PB push_back
#define LL long long
#define eps 1e-10
#define debug puts("**debug**")
using namespace std; const int maxn = 333;
const int T = 1000;
struct Edge
{
int from, to, dist;
};
vector<Edge> edges;
vector<int> G[maxn];
vector<int> g[maxn];
int n, ncase, color[maxn], flag, d[maxn], cnt[maxn];
bool inq[maxn];
char ch[maxn][maxn]; void init()
{
REP(i, n) G[i].clear(), g[i].clear();
edges.clear(); CLR(color, 0);
} void add(int a, int b, int c)
{
edges.PB((Edge){a, b, c});
int nc = edges.size();
G[a].PB(nc-1);
} void dfs(int u, int c) //二分染色
{
color[u] = c;
int nc = g[u].size();
REP(i, nc)
{
int v = g[u][i];
if(!color[v]) dfs(v, 3-c);
}
} bool spfa()
{
queue<int> q;
REP(i, n) d[i] = cnt[i] = 0, inq[i] = 1, q.push(i);
while(!q.empty())
{
int u = q.front(); q.pop();
inq[u] = false;
REP(i, G[u].size())
{
Edge& e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist)
{
d[e.to] = d[u] + e.dist;
if(!inq[e.to])
{
q.push(e.to);
inq[e.to] = true;
if(++cnt[e.to] > n) return true;
}
}
}
}
return false;
} bool solve()
{
//判奇圈
REP(i, n) if(!color[i]) dfs(i, 1);
REP(i, n) REP(j, g[i].size()) if(color[i] == color[g[i][j]]) return 0; REP(i, n)
{
FF(j, i+1, n)
{
if(ch[i][j] == '1')
{
if(color[i] == 1) add(i, j, -T);
else add(j, i, -T);
}
else
{
if(color[i] == 1) add(j, i, T-1);
else add(i, j, T-1);
}
}
}
if(spfa()) return 0;
return 1;
} int main()
{
scanf("%d", &ncase);
while(ncase--)
{
init();
scanf("%d", &n);
REP(i, n)
{
scanf("%s", ch[i]);
REP(j, n) if(i != j && ch[i][j] == '1') g[i].PB(j);
}
if(solve()) puts("Yes");
else puts("No");
}
return 0;
}

hdu 4598 Difference(奇圈判定+差分约束)的更多相关文章

  1. HDU 4598 Difference

    Difference Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  2. HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. HDU 3666 THE MATRIX PROBLEM (差分约束,最短路)

    题意: 给一个n*m矩阵,每个格子上有一个数字a[i][j],给定L和U,问:是否有这样两个序列{a1...an}和{b1...bn},满足 L<=a[i][j]*ai/bj<=U .若存 ...

  4. HDU 3592 World Exhibition(线性差分约束,spfa跑最短路+判断负环)

    World Exhibition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 4598 差分约束

    思路:首先就是判断是否有奇环,若存在奇环,则输出No. 然后用差分约束找是否符合条件. 对于e(i,j)属于E,并且假设顶点v[i]为正数,那么v[i]-v[j]>=T--->v[j]-v ...

  6. HDU 3118 Arbiter 判定奇圈

    题目来源:pid=3118">HDU 3118 Arbiter 题意:翻译过来就是不能有奇圈 每走一步状态会变化 当他回到起点时假设和原来的状态不一样 可能会死 求至少去掉多少条边能够 ...

  7. hdu 1531(差分约束)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...

  8. POJ 1364 / HDU 3666 【差分约束-SPFA】

    POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c      —      sum[a]<=sum[a+b+1]−c−1  ...

  9. POJ 3169 Layout (HDU 3592) 差分约束

    http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...

随机推荐

  1. 第一个MVC模型

    根据慕课网的视频自学来的. 关于MVC的简介和一些常识:http://www.cnblogs.com/jobscn/archive/2011/11/08/2240725.html MVC模式 : MV ...

  2. 分支-15. 日K蜡烛图

    /* * Main.c * 分支-15. 日K蜡烛图 * Created on: 2014年6月18日 * Author: Boomkeeper ****测试通过***** */ #include & ...

  3. win7下设置 WiFi AP

    开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让计算机变成无线路由器.实现共享上网. 1.以管理员身份运行命令提示符: “开始”---在搜索栏输入“cmd”-- ...

  4. CATALINA_BASE与CATALINA_HOME的区别(转)

    到底CATALINA_HOME和CATALINA_BASE有什么区别呢,之前因为都是小打小闹的在服务器上安装一个tomcat就得了,然后根据前人的配置,将CATALINA_HOME和CATALINA_ ...

  5. java代码收藏:获取HttpServletRequest中某一前缀的参数

    public static Map getParametersStartingWith(ServletRequest request, String prefix) { Enumeration par ...

  6. poj3086---数论

    #include <stdio.h> #include <stdlib.h> int T(int n) { ,i; ;i<=n;i++) { sum+=i; } retu ...

  7. Gunner II(二分,map,数字转化)

    Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  8. MySQL存储过程的基本函数

    (1).字符串类 CHARSET(str) //返回字串字符集 CONCAT (string2 [,... ]) //连接字串 INSTR (string ,substring ) //返回subst ...

  9. 服务管理——ntp

    一 ntp相关知识 什么是时间同步服务器 Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可 ...

  10. Secret of Success(成功的秘诀)

    A youngman asked Socrates the secret of Success. Socrates told the youngman to meet him near the riv ...