码迷,mamicode.com
首页 > 其他好文 > 详细

poj2983——差分约束,bellman_ford

时间:2015-04-10 00:59:39      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

poj2983——差分约束,bellman_ford

Is the Information Reliable?
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 11560   Accepted: 3658

Description

The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years ago. Draco established a line of defense called Grot. Grot is a straight line with N defense stations. Because of the cooperation of the stations, Zibu’s Marine Glory cannot march any further but stay outside the line.

A mystery Information Group X benefits form selling information to both sides of the war. Today you the administrator of Zibu’s Intelligence Department got a piece of information about Grot’s defense stations’ arrangement from Information Group X. Your task is to determine whether the information is reliable.

The information consists of M tips. Each tip is either precise or vague.

Precise tip is in the form of P A B X, means defense station A is X light-years north of defense station B.

Vague tip is in the form of V A B, means defense station A is in the north of defense station B, at least 1 light-year, but the precise distance is unknown.

Input

There are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 1000) and M (1 ≤ M ≤ 100000).The next M line each describe a tip, either in precise form or vague form.

Output

Output one line for each test case in the input. Output “Reliable” if It is possible to arrange N defense stations satisfying all the M tips, otherwise output “Unreliable”.

Sample Input

3 4
P 1 2 1
P 2 3 1
V 1 3
P 1 3 1
5 5
V 1 2
V 2 3
V 3 4
V 4 5
V 3 5

Sample Output

Unreliable
Reliable
题意:如题
思路;用spfa须增设超级源点,所以又多了N条边,故TLE,而bellman_ford则可以直接处理
技术分享
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn=10010;
const int INF=(1<<28);

int N,M;
int a,b,x;
struct Edge
{
    int u,v,w;
};Edge edge[maxn*100];int e;
char ch;
int dist[maxn];

void add_edge(int u,int v,int w)
{
    edge[e++]={u,v,w};
}

bool relax(int cur)
{
    int tmp=dist[edge[cur].u]+edge[cur].w;
    if(tmp>dist[edge[cur].v]){
        dist[edge[cur].v]=tmp;
        return true;
    }
    return false;
}

bool bellman_ford()
{
    memset(dist,0,sizeof(dist));
    for(int i=0;i<N;i++){
        bool flag=0;
        for(int j=0;j<e;j++){
            if(relax(j)) flag=1;
        }
        if(!flag) return true;
    }
    for(int i=0;i<e;i++){
        if(relax(i)) return false;
    }
    return true;
}

int main()
{
    while(cin>>N>>M){
        e=0;
        char ch;
        while(M--){
            getchar();
            scanf("%c",&ch);
            if(ch==P){
                //cin>>a>>b>>x;
                scanf("%d%d%d",&a,&b,&x);
                add_edge(b,a,x);
                add_edge(a,b,-x);
            }
            else{
                scanf("%d%d",&a,&b);
                add_edge(b,a,1);
            }
        }
        if(bellman_ford()) cout<<"Reliable"<<endl;
        else cout<<"Unreliable"<<endl;
    }
    return 0;
}
View Code

 

poj2983——差分约束,bellman_ford

标签:

原文地址:http://www.cnblogs.com/--560/p/4413013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!