zoukankan      html  css  js  c++  java
  • poj 3259 Wormholes(Bellman_ford)

    题意:农民john有N快田,M条路,w个虫洞,虫洞的功能回到多少分钟之前。

    思路:想法很简单,将w个虫洞的值存为负值,然后求图中是否有负环路,如果有就输出“YES”,否则输出“NO”。但是题目中有重复的路要滤除,还有这题有连接表存储比用邻接矩阵存储有简单。

    代码:

    View Code
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <math.h>
    #define  N 505
    #define  INF 10006
    using namespace std ;
    
    struct node
    {
        int  s , e , v ;
    }p[10004] ;
    int num ;
    
    void add( int x , int y , int z )
    {
        p[num].s = x ;
        p[num].e = y ;
        p[num].v = z ;
        num++ ;
    }
    
    int main()
    {
        int cas , n , m , w ;
        int x, y , z ,i , j ;
        int dis[N] ;
    
        scanf ( "%d" , &cas );
        while ( cas-- )
        {
            scanf ( "%d%d%d" , &n , &m , &w );
            for ( i = 1 ; i <= n ; i++ )
            {
                dis[i] = INF ;
            }
    
            num = 0 ;
            for ( i = 1 ; i <= m ; i++ )
            {
                scanf ( "%d%d%d" , &x , &y , &z ) ;
                add( x , y , z ) ;
                add( y , x , z ) ;
            }
            for ( i = 1 ; i <= w ; i++ )
            {
                scanf ( "%d%d%d" , &x , &y , &z ) ;
                add( x , y , -z ) ;
            }
            dis[1] = 0 ;
            for ( i = 1 ; i < n ; i++ )
                for ( j = 0 ; j < num ; j++ )
                if ( dis[p[j].e] > dis[p[j].s] + p[j].v )
                {
                    dis[p[j].e] = dis[p[j].s] + p[j].v ;
                }
            int flag = 0 ;
            for ( i = 0 ; i < num ; i++ )
            if ( dis[p[i].e] > dis[p[i].s] + p[i].v )
            {
                flag = 1 ; break ;
            }
            if ( flag )
            printf ( "YES\n" );
            else
            printf ( "NO\n" );
        }
        return 0 ;
    }
  • 相关阅读:
    递归算法的时间复杂度分析
    MongoDB入门简单介绍
    关于用例须要多少文档以及业务用例等等
    Java连接redis的使用演示样例
    C++ String 转 char*
    MySQL和PostgreSQL 导入数据对照
    SSL连接建立过程分析(1)
    XTU OJ 1210 Happy Number (暴力+打表)
    Codeforces Round #258 (Div. 2)[ABCD]
    CreateFont具体解释
  • 原文地址:https://www.cnblogs.com/misty1/p/2722822.html
Copyright © 2011-2022 走看看