题目大意:
给出n个防御站,m个不等式,推断是否有可行解
思路:
这是一道关于查分约束的题。解题的关键就是找到不等关系。构成不等式组,然后用SPFA推断是否具有负环
P A B X : Dis[a] = Dis[b] + X;
V A B : Dis[a] = Dis[b] + 1;
即:
P: Dis[a] - DIs[b] <= X
Dis[b] - Dis[a] <= -X
V: Dis[a] - Dis[b] <=1
因为是用的SPFA算法。建立的图中的每个点不一定是联通的。所以须要建立一个虚点将全部的点连接
/* ***********************************************
Author :sunming
Created Time :2016年02月18日 星期四 15时46分46秒
File Name :a.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
//#include <time.h>
//#include <list.h>
//#include <vector.h>
using namespace std;
#pragma comment(linker, "/STACK:1024000000")
#define LL long long
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=100100+10;
struct node
{
int v,w,next;
} Edge[MAXN*3+10];
int Dis[1010],head[1010],cnt[1010];
bool vis[1010];
int top;
void Add(int u, int v, int w)
{
Edge[top].v = v;
Edge[top].w = w;
Edge[top].next = head[u];
head[u] = top++;
}
bool SPFA(int n)
{
queue<int >Q;
for(int i=0; i<=n; i++)
{
Dis[i] = INF;
cnt[i] = 0;
vis[i] = false;
}
Dis[0] = 0;
Q.push(0);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
if(++cnt[u] > n)
return false;
vis[u] = false;
for(int i=head[u]; i!=-1; i=Edge[i].next)
{
int v = Edge[i].v;
int w = Edge[i].w;
if(Dis[v] > Dis[u] + w)
{
Dis[v] = Dis[u] + w;
if(!vis[v])
{
vis[v] = true;
Q.push(v);
}
}
}
}
return true;
}
int main()
{
int n,m,u,v,w;
while(~scanf("%d%d",&n,&m))
{
memset(head, -1, sizeof(head));
top = 0;
while(m--)
{
char str[2];
scanf("%s%d%d",str,&u,&v);
if(!strcmp(str,"P"))
{
scanf("%d",&w);
Add(u,v,w);
Add(v,u,-w);
}
else
Add(v,u,-1);
}
for(int i=1; i<=n; i++)
Add(0,i,0);
if(SPFA(n))
printf("Reliable\n");
else
printf("Unreliable\n");
}
return 0;
}
版权声明:本文为博主原创文章。未经博主同意不得转载。
举报
- 本文已收录于下面专栏:
相关文章推荐
-
POJ 2983 Is the Information Reliable(差分约束系统 spfa判负环)
POJ 题目大意在一条坐标轴上有n个点。如今给出m组描写叙述,每组描写叙述是一下面两种形式之中的一个: P A B X V A B第一种精确(Precise)表示点A在点B的右边X个位置。另外一种模糊(Vague)仅仅...
- mmy1996
- 2017-02-17 18:47
- 64
-
POJ-2983 用SPFA求解差分约束..
同上..用SPFA来解决...用SPFA的第一个问题是怎样跳出while..由于这题明显的可能有负环..SPFA假设普通的...有负环..则会将环上的点不断入队列..就会死循环!!为了能跳出死循环或者说能推断出有负环..就用个数组来记录每一个点入队的次数...假设入队的次数超过点的总数N了..则能够推断出是出现负环了..这个思想是从Bellman-Ford那来的..回忆Bellman-Ford..外围是用来循环次数..是循环了N次..第二层则循环全部的边..能够发现一个点最多就更新循环次数N了... 再一个要注意的是用SPFA就必须先要确定源点然后入队...但这里假设任
- huobengle
- 2011-10-09 19:43
- 300
-
学习笔记----差分约束系统 POJ 2983 Is the Information Reliable?
总结一下:假设一个系统由n个变量和m个约束条件组成,当中每一个约束条件形如xj-xi 求解差分约束系统。能够转化成图论的单源最短路径(或最长路径)问题。 观察xj-xihttp://baike.ba...
- flyljg
- 2014-08-22 21:44
- 284
-
[差分约束]poj 2983:Is the Information Reliable?
大致题意: 给出n个点的m条约束信息。每条信息表述为(P a b c)表示a在b北方距离c的位置,或者(V a b) 表示a在b北方1单位距离或者更远的位置。
问是否可能存在符合以上m个要求的点。 大致思路: 把dis[i]设为其到始点的距离。
第二个条件非常easydis[a]-dis[b]>=1 也就是dis[b]<=dis[a]-1。对于第一个。带等于号的条件dis[a]-dis[b]==c,我们能够转化为dis[a]-dis[b]>=c和dis[a]-dis[b]<=c 两个不等式,然后再转化为最短

- 暴风雪
- 2012-01-21 03:17
- 640
-
POJ 2983(差分约束-有无解)
差分约束…… Program p2983; var n,m,i,j,p:longint; c:char; flag:boolean; d:array[0..5000] of longint; w:array[1..100100,1..3] of longint; procedure relax(v,u,w:longint); begin if (d[v]>d[u]+w) then begin d[v]:=d[u]+
- lovnet
- 2012-08-16 18:37
- 364
-
学习笔记----差分约束系统初步 POJ 2983 Is the Information Reliable?
首先摘一段来自于百度百科的解释:假设一个系统由n个变量和m个约束条件组成。当中每一个约束条件形如xj-xi 求解差分约束系统,能够转化成图论的单源最短路径(或最长路径)问题。 观察xj-xihttp:/...
- xu12110501127
- 2013-11-27 11:16
- 733
-
poj1364,2983-差分约束系统
poj1364 题目大意:如今如果有一个这种序列,S={a1,a2,a3。a4...ai...at} 当中ai=a*si,事实上这句能够忽略不看 如今给出一个不等式。使得ai+a(i+1)+a(i+2)+...+a(i+n)<ki或者是ai+a(i+1)+a(i+2)+...+a(i+n)>ki 首先给出两个数分别代表S序列有多少个,有多少个
- 人生难得糊涂
- 2014-08-12 21:58
- 214
-
POJ 2983 Is the Information Reliable (差分约束 bell/spfa推断负环)
Is the Information Reliable?Time Limit: 3000MS Memory Limit: 131072K Total Submis...

- qq_34374664
- 2017-07-21 00:52
- 67
-
POJ 2983 Is the Information Reliable?(差分约束系统)
POJ 2983 Is the Information Reliable?(差分约束系统) http://
- 阿尔萨斯
- 2014-07-18 14:20
- 65
-
[POJ 2983]Is the Information Reliable?[差分约束]
题目链接:[POJ 2983]Is the Information Reliable?[差分约束] 题意分析: 给出N个防御站和M个提示。提示P代表A站在B站的北方X米处,提示V代表。A站在B站北...
- CatGlory
- 2016-03-21 19:12
- 121
收藏助手
不良信息举报
浙公网安备 33010602011771号
0条评论