Bellman算法PKU 3259

Wormholes

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 31593   Accepted: 11497

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..NM (1 ≤ M ≤ 2500) paths, andW (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, FF farm descriptions follow.

Line 1 of each farm: Three space-separated integers respectively: NM, and W

Lines 2..M+1 of each farm: Three space-separated numbers (SET) 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 (SET) 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

Hint

For farm 1, FJ cannot travel back in time.

For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
//#include <map>
using namespace std;

const int VM = 520;
const int EM = 5020;
const int INF = 0x3f3f3f3f;
//const int INF = 0x3f3f3f3f;

struct Edge{
    int u,v;
    int w;
}edge[EM<<1];

int N,M,W,cnt,dis[VM];

void addage(int cu,int cv,int cw){
    edge[cnt].u = cu;
    edge[cnt].v = cv;
    edge[cnt].w = cw;
    cnt++;
}

int Bellman(){
    int i,j,flag;
    for(i=0;i<N;i++){
        dis[i] = INF;
    }
    for(i=1;i<N;i++){
        flag = 1;
        for(j=0;j<cnt;j++){
            if(dis[edge[j].u] > dis[edge[j].v]+edge[j].w){
                dis[edge[j].u] = dis[edge[j].v]+edge[j].w;
                flag = 0;
            }
        }
        if(flag){
            break;
        }
    }
    for(i=0;i<cnt;i++){
        if(dis[edge[i].u] > dis[edge[i].v]+edge[i].w){
            return 1;
        }
    }

    return 0;
}

int main(){
    int t;
    int i;

    while(~scanf("%d",&t)){
        while(t--){
            scanf("%d%d%d",&N,&M,&W);
            cnt = 0;
            int u,v,w;
            for(i=0;i<M;i++){
                scanf("%d%d%d",&u,&v,&w);
                addage(u,v,w);
                addage(v,u,w);
            }
            for(i=0;i<W;i++){
                scanf("%d%d%d",&u,&v,&w);
                addage(u,v,-w);
            }
            if(Bellman()){
                printf("YES\n");
            }
            else{
                printf("NO\n");
            }
        }
    }

    return 0;
}
时间: 2024-10-15 16:42:34

Bellman算法PKU 3259的相关文章

最短路径(bellman算法详解)

首先介绍一下bellman算法: Bellman-ford算法是求含负权图的单源最短路径算法,效率很低,但代码很容易写.即进行持续地松弛(原文是这么写的,为什么要叫松弛,争议很大),每次松弛把每条边都更新一下,若n-1次松弛后还能更新,则说明图中有负环,无法得出结果,否则就成功完成.Bellman-ford算法有一个小优化:每次松弛先设一个标识flag,初值为FALSE,若有边更新则赋值为TRUE,最终如果还是FALSE则直接成功退出.Bellman-ford算法浪费了许多时间做没有必要的松弛,

最短路径问题——bellman算法

关于最短路径问题,最近学了四种方法——bellman算法.邻接表法.dijkstra算法和floyd-warshall算法. 这当中最简单的为bellman算法,通过定义一个边的结构体,存储边的起点.终点和路径长度,然后通过一个while(1)死循环不断地访问每一条边,更新源点到各点的最短距离,直到没有更新时结束.这时便得到了从源点到其他点的最短距离.附上代码一段: #include<iostream>#define INF 100000000using namespace std; stru

Bellman 算法

这道题目其实就是在求有没有正环.与求负环的区别就是要不断的更新值,但是这个值要变大.而不是变小. Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20441   Accepted: 7337 Description Several currency exchange points are working in our city. Let us suppose that each poin

Bellman算法

#include<stdio.h> #include<iostream> #define maxn 1000 #define inf 0x3fffffff using namespace std; struct Edage { int from; int to; int cost; }es[maxn]; int d[maxn]; int v,e; //无负圈,求单源最短路 /* 单源最短路:从一个点s到其他点的最短距离 */ void shortest_path(int s) {

Bellman算法模板

1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int inf=1<<29; 6 const int N=1001; 7 int w[N][N],d[N]; 8 int u,v,c,m,n; 9 struct node 10 { 11 int from; 12 int to; 13 int c; 14 }e[N*N

Poj 3259 Wormholes

1.Link: http://poj.org/problem?id=3259 2.Content: Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32078   Accepted: 11651 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A w

轨迹压缩之Douglas-Peucker算法之Java实现

第一部分 问题描述 1.1 具体任务 本次作业任务是轨迹压缩,给定一个GPS数据记录文件,每条记录包含经度和维度两个坐标字段,所有记录的经纬度坐标构成一条轨迹,要求采用合适的压缩算法,使得压缩后轨迹的距离误差小于30m. 1.2 程序输入 本程序输入是一个GPS数据记录文件. 1.3 数据输出 输出形式是文件,包括三部分,压缩后点的ID序列及坐标.点的个数.平均距离误差.压缩率 第二部分 问题解答 根据问题描述,我们对问题进行求解,问题求解分为以下几步: 2.1 数据预处理 本次程序输入为GPS

算法训练 最短路

时间限制:1.0s   内存限制:256.0MB 问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从1号点到其他点的最短路(顶点从1到n编号). 输入格式 第一行两个整数n, m. 接下来的m行,每行有三个整数u, v, l,表示u到v有一条长度为l的边. 输出格式 共n-1行,第i行表示1号点到i+1号点的最短路. 样例输入 3 31 2 -12 3 -13 1 2 样例输出 -1-2 数据规模与约定 对于10%的数据,n = 2,m = 2. 对于

nyoj 115------城市平乱( dijkstra // bellman )

城市平乱 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 南将军统领着N个部队,这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安,这M个城市分别编号从1到M. 现在,小工军师告诉南将军,第K号城市发生了暴乱,南将军从各个部队都派遣了一个分队沿最近路去往暴乱城市平乱. 现在已知在任意两个城市之间的路行军所需的时间,你作为南将军麾下最厉害的程序员,请你编写一个程序来告诉南将军第一个分队到达叛乱城市所需的时间. 注意,两个城市之间可能不只一条路.