zoukankan      html  css  js  c++  java
  • POJ 3259 Wormholes(Bellman Ford 单源最短路径(可求带负权边的))

    Description

    While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

    As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

    To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

    Input

    Line 1: A single integer, F. F farm descriptions follow.
    Line 1 of each farm: Three space-separated integers respectively: N, M, and W
    Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
    Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

    Output

    Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

    Sample Input

    2
    3 3 1
    1 2 2
    1 3 4
    2 3 1
    3 1 3
    3 2 1
    1 2 3
    2 3 4
    3 1 8

    Sample Output

    NO
    YES
    //第一个Bellman Ford 算法。感觉不熟悉、对这个算法的实现不是怎么理解
    //这题是看了别人的代码才会写的 ,Path双边,虫洞单向,现在还不明白为什么随便选择个边作起点都可以AC
    //这题是利用 Bellman Ford 判断有从起点S可到达的负回路
    #include
     <string.h> #include <stdio.h> 
    #include <iostream>
    #define N 6666
    #define Max 0xfffffff
    using namespace std;
    struct node
    {
    int s,e,t;
    }Edge[N]; int d[503]; int n,m,w;
    bool bellman_ford() {
    int i,j,temp; d[1]=0;
    for(i=2;i<=n;i++)
    d[i]=Max; for(i=1;i<n;i++)
    for(j=1;j<=w;j++) {
    temp=Edge[j].t+d[Edge[j].s];
    if(d[Edge[j].s]!=Max&&d[Edge[j].e]>temp)
    d[Edge[j].e]=temp;
    }
    for(i=1;i<=w;i++)
    if(d[Edge[i].s]!=Max&&d[Edge[i].e]>Edge[i].t+d[Edge[i].s])
    return 0;
    return 1;
    }
    int main() {
    // freopen("in.txt","r",stdin);
    int i,j,t; scanf("%d",&t);
    while(t--) {
    j=0;
    scanf("%d%d%d",&n,&m,&w);
    for(i=1;i<=m;i++) {
    ++j;
    scanf("%d%d%d",&Edge[j].s,&Edge[j].e,&Edge[j].t);
    ++j;
    Edge[j].s=Edge[j-1].e;
    Edge[j].e=Edge[j-1].s;
    Edge[j].t=Edge[j-1].t;
    } for(i=1;i<=w;i++) {
    ++j; scanf("%d%d%d",&Edge[j].s,&Edge[j].e,&Edge[j].t);
    Edge[j].t=-Edge[j].t;
    } w=j;
    if(!bellman_ford())
    printf("YES\n");
    else printf("NO\n");
    }
    return 0;
    }
  • 相关阅读:
    hdu 1251(字典树)
    hdu 1556(树状数组)
    hdu 3275(线段树的延迟标记,我被坑了)
    TCL之容器
    Codeforces Round #587 (Div. 3) D. Swords
    struts2中多个文件同时上传
    ffmpeg的使用
    struts2中类型转换器
    struts中访问servlet API的方法
    struts2中多个逻辑action(方法)的动态调用
  • 原文地址:https://www.cnblogs.com/372465774y/p/2460586.html
Copyright © 2011-2022 走看看