http://poj.org/problem?id=2983

题意:给出M条信息,判断这些信息的正确性。(1)V A B :表示A,B之间的距离>=1; (2)P A B X :表示A B之间的距离为x。

思路:dis[i]表示i到原点的距离,由(1)知 dis[A]<=dis[B]-1 即B->A之间有一条边,权值为-1;由(2)知: dis[A]<=dis[B]-x && dis[B] <= dis[A]+x,即A->B的权值为-x,B->A的权值为x。增加一个超级源点,与所有的点相连且权值为0.建图,spfa判断是否有负环(即判断进站次数,若大于点数则存在负环)。若存在负环则信息为假,否则为真。

 #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int N=;
const int INF=<<;
int head[],vis[];
int dis[N],num[];
int cnt,n; struct node
{
int u,v,w;
int next;
} edge[N];
void init()
{
cnt = ;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
}
void add(int u,int v,int w)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int spfa()
{
queue<int>q;
for (int i = ; i <= n; i ++)
dis[i] = INF;
dis[] = ;
q.push();
vis[] = ;
while(!q.empty())
{
int u = q.front();
vis[u] = ;
q.pop();
for (int j = head[u]; j!=-; j=edge[j].next)
{
int v = edge[j].v;
int w = edge[j].w;
if (dis[v] > dis[u]+w)
{
dis[v] = dis[u]+w;
if (!vis[v])
{
q.push(v);
vis[v] = ;
++num[v];
if (num[v] > n)
return ;
}
}
}
}
return ;
}
int main()
{
int m,u,v,w;
char s[];
while(~scanf("%d%d",&n,&m))
{
init();
for (int i = ; i <= n; i++)
add(,i,);
for (int i = ; i < m; i++)
{
scanf("%s",s);
if (s[]=='P')
{
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
add(v,u,-w);
}
if (s[]=='V')
{
scanf("%d %d",&u,&v);
add(v,u,-);
}
}
if (spfa())
printf("Reliable\n");
else
printf("Unreliable\n");
}
return ;
}

Is the Information Reliable?(差分约束系统)的更多相关文章

  1. POJ 2983-Is the Information Reliable?(差分约束系统)

    题目地址:POJ 2983 题意:有N个车站.给出一些点的精确信息和模糊信息.精确信息给出两点的位置和距离.模糊信息给出两点的位置.但距离大于等于一.试确定是否全部的信息满足条件. 思路:事实上就是让 ...

  2. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  3. 【POJ 2983】Is the Information Reliable?(差分约束系统)

    id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...

  4. poj2983--Is the Information Reliable?(差分约束)

    Is the Information Reliable? Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 11125   A ...

  5. Burn the Linked Camp(bellman 差分约束系统)

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  6. POJ2983 Is the Information Reliable?

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=267#problem/B                        B -  ...

  7. 【POJ 2983】 Is the information reliable?

    [题目链接] 点击打开链接 [算法] 差分约束系统,SPFA判负环 [代码] #include <algorithm> #include <bitset> #include & ...

  8. UVA11478 Halum [差分约束系统]

    https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的 ...

  9. BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】

    2330: [SCOI2011]糖果 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5395  Solved: 1750[Submit][Status ...

随机推荐

  1. css--小白入门篇3

    一.上篇知识复习 css属性,面试的时候会有笔试,笔试没有智能感应的: ● 加粗,倾斜,下划线: 1 font-weight:bold; 2 font-style:italic; 3 text-dec ...

  2. js的title提示

    $(function() { //先在页面创建一个层 var jqtip = $("<div id='jqtip20130719'" + "style='paddi ...

  3. python 使用time / datetime进行时间、时间戳、日期转换

    python 使用time 进行时间.时间戳.日期格式转换 #!/usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2017/11/7 15:53 # ...

  4. JQurey---新尝试

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. JAVA实现创建Excel表并导出(转发)

    <span style="font-family:Verdana, Arial, Helvetica, sans-serif;line-height:25.2px;background ...

  6. 斯特林公式 hdu1018

    杭电上面1018>>点击测试<< 思路:当问到阶乘的值时候,用万进制来写:但是问阶乘值的位数的时候,就可以用斯特林公式了 log10(2*pi*n)/2+n*log10(n/e ...

  7. vue.js编程式路由导航 --- 由浅入深

    编程式路由导航 实例中定义一个方法,这个方法绑定在标签上 然后就设置路由跳转 语法 this.$router.history.push('要跳转路由的地址') <!DOCTYPE html> ...

  8. Javaweb中文乱码问题

    request.setCharacterEncoding("utf-8");必须写在获得参数之前,即request.getParameter();之前

  9. [bzoj1833][ZJOI2010]数字计数(数位DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1833 分析:简单的数位DP f[i][j][k]表示在i位数.最高位j的所有数字中k的 ...

  10. CH上的Think Bear#1模拟赛

    题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20%20Thinking%20Bear%20%231%20(NOIP%E6%A8 ...