zoukankan      html  css  js  c++  java
  • HDU3592 World Exhibition 差分约束

    World Exhibition

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 568    Accepted Submission(s): 264


    Problem Description

    Nowadays, many people want to go to Shanghai to visit the World Exhibition. So there are always a lot of people who are standing along a straight line waiting for entering. Assume that there are N (2 <= N <= 1,000) people numbered 1..N who are standing in the same order as they are numbered. It is possible that two or more person line up at exactly the same location in the condition that those visit it in a group.

    There is something interesting. Some like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of X (1 <= X <= 10,000) constraints describes which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person dislike each other and the minimum distance by which they must be separated.

    Your job is to compute, if possible, the maximum possible distance between person 1 and person N that satisfies the distance constraints.
     

    Input

    First line: An integer T represents the case of test.

    The next line: Three space-separated integers: N, X, and Y.

    The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.

    The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.
     

    Output

    For each line: A single integer. If no line-up is possible, output -1. If person 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between person 1 and N.
     

    Sample Input
    1
    4 2 1
    1 3 8
    2 4 15
    2 3 4
     

    Sample Output
    19
     
      该题是把所有的关系全部列出来,刚开始把该题理解为无向图,结果果断错,其实该题是一个有向图。因为在计算排队的时侯,没有回去的可能,如果中间有不连续的约束条件则和输出-2,即1和N之间没有路相连接,输出-1的情况是出现了负环。 这题不知到为什么,在构边时使用逆邻接表就能过,邻接表一直WA。
      代码如下:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <cstdlib>
    #include <vector>
    #include <queue>
    #define INF 0x7f7f7f7f
    using namespace std;
    
    struct Node
    {
    	int pos, dis;
    }info;
    
    int dis[1005], ti[1005], inqu[1005];
    
    bool SPFA( int N, vector< Node >vec[] )
    {
    	queue< int >q;
    	vector< Node >::iterator it;
    	for( int i= 0; i<= N; ++i )
    	{
    		dis[i]= INF;
    		ti[i]= 0;
    		inqu[i]= 0;
    	}
    	dis[1]= 0;
    	q.push( 1 );
    	inqu[1]= 1;
    	while( !q.empty() )
    	{
    		int pos= q.front();
    		q.pop();
    		inqu[ pos ]= 0;
    		for( it= vec[ pos ].begin(); it!= vec[ pos ].end(); ++it )
    		{ 
    			if( dis[ it->pos ]> dis[pos]+ it->dis )
    			{
    				dis[ it->pos ]= dis[pos]+ it->dis;
    				if( !inqu[ it->pos ] )
    				{
    					q.push( it->pos );
    					inqu[ it->pos ]= 1;
    					ti[ it->pos ]++;
    					if( ti[ it->pos ]> N )
    					{ 
    						return false;
    					}
    				}
    			}
    		}
    	}
    	return true;
    }
    
    int main(  )
    {
    	int T, x, y, z, N, X, Y;
    	scanf( "%d", &T );
    	while( T-- )
    	{
    		scanf( "%d %d %d", &N, &X, &Y );
    		vector< Node >vec[1005];
    		for( int i= 1; i<= X; ++i )
    		{
    			scanf( "%d %d %d", &x, &y, &z );
    			info.pos= y, info.dis= z;
    			vec[x].push_back( info );
    		}
    		for( int i= 1; i<= Y; ++i )
    		{
    			scanf( "%d %d %d", &x, &y, &z );
    			info.pos= x, info.dis= -z;
    			vec[y].push_back( info );
    		}
    /*		for( int i= 2; i<= N; ++i )
    		{
    			info.pos= i, info.dis= 0;
    			vec[i- 1].push_back( info );
    		}    */  // 不加也能过
    		if( SPFA( N, vec ) )
    		{
    			if( dis[N]== INF )
    			{
    				printf( "-2\n" );
    			}
    			else
    			{
    				printf( "%d\n", dis[N]- dis[1] );
    			}
    		}
    		else
    		{
    			printf( "-1\n" );
    		}
    	}
    	return 0;
    }
    

  • 相关阅读:
    poj2926
    poj2917
    新手入门:PHP网站开发中常见问题汇总
    js字符串转换成数字,数字转换成字符串
    !! jquery主要资料
    IT公司面试手册.htmhttp://www.mianwww.com/html/2009/02/2931.html
    sql语句 返回最后的insert操作所产生的自动增长的id
    eclipse 无法正常保存js
    javascript深入理解js闭包
    如何用javascript操作本地文件(读写txt文件)
  • 原文地址:https://www.cnblogs.com/Lyush/p/2129543.html
Copyright © 2011-2022 走看看