zoukankan      html  css  js  c++  java
  • poj_3259Wormholes

    Wormholes
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 24055   Accepted: 8573

    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, 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, 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
    判断有无负环。。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    const int INF = 999999;  
    const int MAXN = 10005;
    
    typedef struct Node
    {
    	int v;//起点
    	int u;//终点
    	int w; 
    }Node;
    Node edge[MAXN];
    int dist[MAXN];     //此处要特别注意,bellman-ford算法中不要使用0x7fffffff,为此wa了n次 
    int edgenum, n, m, w;
    
    bool BellmanFord(int s)
    {
    	int i, j;
    	bool flag = false;
    	for(i = 1; i <= n; ++i)
    	{
    		dist[i] = INF;        //其余点的距离设置为无穷
    	}
    	dist[s] = 0;             //源点的距离设置为0
    	for(i = 1; i < n; ++i)
    	{
    		flag = false;       //优化:如果某次迭代中没有任何一个d值改变,尽可以立刻退出迭代而不需要把所有的n-1次迭代都做完
    		for(j = 0; j < edgenum; ++j)
    		{
    			if(dist[edge[j].v] > dist[edge[j].u] + edge[j].w)
    			{
    				flag = true;
    				dist[edge[j].v] = dist[edge[j].u] + edge[j].w;
    			}
    		}
    		if(!flag)
    		{
    			break;
    		}
    	}
    	for(i = 0; i < edgenum; ++i)
    	{
    		if(dist[edge[i].v] > dist[edge[i].u] + edge[i].w)
    		{
    			return false;//存在负环
    		}
    	}
    	return true;//不存在负环
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int t, x, y, z;
    	scanf("%d", &t);
    	while (t--)
    	{
    		edgenum = 0;
    		scanf("%d %d %d", &n, &m, &w);
    		for(int i = 1; i <= m; i++)
    		{
    			scanf("%d %d %d", &x, &y, &z);
    			edge[edgenum].u = x;
    			edge[edgenum].v = y;
    			edge[edgenum++].w = z;
    			edge[edgenum].u = y;
    			edge[edgenum].v = x;
    			edge[edgenum++].w = z;
    		}
    		for(int i = 1; i <= w; i++)
    		{
    			scanf("%d %d %d", &x, &y, &z);
    			edge[edgenum].u = x;
    			edge[edgenum].v = y;
    			edge[edgenum++].w = -z;
    		}
    		if(BellmanFord(1))
    		{
    			printf("NO\n");
    		}
    		else
    		{
    			printf("YES\n");
    		}
    	}
    	return 0;
    }




  • 相关阅读:
    css中vertical-align(垂直对齐)的使用
    CSS教程:div垂直居中的N种方法[转]
    前后端分离开发部署模式
    <a>标签的href和onclick属性
    css 字体样式
    谷歌开发者工具界面介绍
    cps和dsp渠道手法的研究
    网络资源汇总
    DataWorks(数据工场)
    vue入门学习笔记
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834992.html
Copyright © 2011-2022 走看看