zoukankan      html  css  js  c++  java
  • codeforces 601A The Two Routes(最短路 flody)

    A. The Two Routes
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.

    A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.

    You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.

    Under these constraints, what is the minimum number of hours needed for both vehicles to reach townn (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.

    Input

    The first line of the input contains two integers n and m (2 ≤ n ≤ 400, 0 ≤ m ≤ n(n - 1) / 2) — the number of towns and the number of railways respectively.

    Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 ≤ u, v ≤ nu ≠ v).

    You may assume that there is at most one railway connecting any two towns.

    Output

    Output one integer — the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output  - 1.

    Examples
    input
    4 2
    1 3
    3 4
    output
    2
    input
    4 6
    1 2
    1 3
    1 4
    2 3
    2 4
    3 4
    output
    -1
    input
    5 5
    4 2
    3 5
    4 5
    5 1
    1 2
    output
    3
    Note

    In the first sample, the train can take the route  and the bus can take the route . Note that they can arrive at town 4 at the same time.

    In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.

     题意:n个城市,m条铁路,没有铁路的地方都是公路,火车只能走铁路,汽车只能走公路,现在要求火车汽车同时从1城市出发,问最短经过多长时间火车汽车同时到达n城市,   注意:在中间通过的城市汽车火车不能同时到达,但是他们都可以在某个城市停下休息任意时间;

    题解:同时求出汽车火车的最短路输出大的一个即可(因为我们可以将较小的那个最短路认为它在起点等待较慢的那个车)

    #include<stdio.h>
    #include<string.h>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<cstdio> 
    #include<string>
    #include<math.h>
    #include<algorithm>
    #define LL long long
    #define PI atan(1.0)*4
    #define DD double
    #define MAX 450
    #define mod 10003
    #define dian 1.000000011
    #define INF 0x3f3f3f3f
    using namespace std;
    int ma[MAX][MAX],s[MAX][MAX]; 
    //int dis[MAX][MAX],dis1[MAX][MAX];
    int n,m;
    void floyd()
    {
    	int i,j,k;
    	for(k=1;k<=n;k++)
    	{
    		for(i=1;i<=n;i++)
    		{
    			for(j=1;j<=n;j++)
    			{
    				ma[i][j]=min(ma[i][j],ma[i][k]+ma[k][j]);
    				s[i][j]=min(s[i][j],s[i][k]+s[k][j]);
    			}
    		}
    	}
    	return ;
    }
    void init()
    {	
    	int i,j;
    	for(i=1;i<=n;i++)
    	{
    		for(j=1;j<=n;j++)
    		{
    			if(i==j)
    				ma[i][j]=s[i][j]=0;
    			else 
    			    ma[i][j]=INF;
    		}
    	}
    }
    int main()
    {
    	int i,j,a,b;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		init();
    		while(m--)
    		{
    			scanf("%d%d",&a,&b);
    			ma[a][b]=ma[b][a]=1;
    		}
    		for(i=1;i<=n;i++)
    		{
    			for(j=1;j<=n;j++)
    			{
    				if(i==j) continue;
    				if(ma[i][j]!=INF) 
    				    s[i][j]=INF;
    				else
    				    s[i][j]=1;
    			}
    		}
    		floyd();
    		if(ma[1][n]!=INF&&s[1][n]!=INF)
    		    printf("%d
    ",max(ma[1][n],s[1][n]));
    		else 
    		    printf("-1
    ");
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    玩转Visual Studio Editor篇
    .Net2.0的集合操作 What i know?
    Log文件压缩
    [转]比较高效地实现从两个不同数组中提取相同部分组成新的数组(只支持Int类型) [C#]
    Stream 和 Byte[]互操作
    net 2.0 中如何在线程引发的事件中控制forms controls
    C# 操作Word文档(转)
    利用Lucene.net搜索引擎进行多条件搜索的做法
    c# 添加图片水印,可以指定水印位置+生成缩略图
    SDN第一次作业
  • 原文地址:https://www.cnblogs.com/tonghao/p/5364992.html
Copyright © 2011-2022 走看看