zoukankan      html  css  js  c++  java
  • poj2983——差分约束,bellman_ford

    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
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    python 的基础 学习 第六天 基础数据类型的操作方法 字典
    python 的基础 学习 第五天 基础数据类型的操作方法
    python 的基础 学习 第四天 基础数据类型
    ASP.NET MVC 入门8、ModelState与数据验证
    ASP.NET MVC 入门7、Hellper与数据的提交与绑定
    ASP.NET MVC 入门6、TempData
    ASP.NET MVC 入门5、View与ViewData
    ASP.NET MVC 入门4、Controller与Action
    ASP.NET MVC 入门3、Routing
    ASP.NET MVC 入门2、项目的目录结构与核心的DLL
  • 原文地址:https://www.cnblogs.com/--560/p/4413013.html
Copyright © 2011-2022 走看看