Codeforces 602 C The Two Routes


The Two Routes

Floyed-Warshall [弗洛伊德算法]

Floyed-Warshall:适用于规模小的图,如果存在负权,需要判断负圈。

权值:类似于从节点 s 到 j 依次经过的长度之和。

类似动态规划:从 s 到 t 的过程程中我们考虑是走还是不走,然后取两者的最小权,最终 s 到 t 的最小权值之和就是我们要求的最短路径。

做出如下类比:每个点看成一个灯,初始时每个灯都是熄灭状态,结点之间的权值初始化成无穷大。然后依次计算出两个连通结点之间的距离,灯依次亮起,直到所有的灯光亮起,计算结束。

判断负圈: g r a p h [ i ] [ i ] graph[ i ][ i ] graph[i][i] i i i 到外面绕一圈回来的最小路径,if( g r a p h [ i ] [ i ] graph[ i ][ i ] graph[i][i] < 0 ) g r a p h [ i ] [ i ] graph[ i ][ i ] graph[i][i] = 0

好处:避免陷入负圈中走不出来。

Floyed 采用三重循环,复杂度 O O O ( n 3 n^3 n3)

for(k=1;k<=n;k++)
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            if(i!=j && j!=k)//松弛操作,使得f[i][j]是最短的
                f[i][j]=min(f[i][j],f[i][k]+f[k][j]);

The Two Routes

题目大意
In Absurdistan, there are n n n towns (numbered 1 1 1 through n n n) and m m m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x x x and y y y, there is a bidirectional road between towns x x x and y y 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 1 1 at the same time. They both have the same destination, town n n n, and don’t make any stops on the way (but they can wait in town n n 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 n n) simultaneously.

Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n n n (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 n 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 n n ≤ 400, 0 ≤  m m m ≤   n ( n   −   1 ) 2 \frac{n(n - 1)}{2} 2n(n1)) — 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 u, v u,v ≤  n , u   ≠   v n, u ≠ v n,u=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 n n. If it’s impossible for at least one of the vehicles to reach town n n n, output  -  1 1 1.
输入样例

4 2
1 3
3 4

输出样例

2

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.

题意

n n n 个点, m m m 条铁路, x x x y y y 之间没有铁路相连就有道路,汽车只能走道路,火车只能走铁路,问汽车和火车能否从 1 出发到达 n n n这个点,如果无法到达,输出 - 1 1 1,如果能,那么两辆车最晚多长时间到达N。两辆车同一时间不能到达同一点(除终点)

题解

看 1 到 n n n 如果有铁路相连,就看汽车,把铁路的值赋成正无穷,道路值为 1,如果没有直接相连的铁路,那么就把汽车走的道路赋成正无穷,铁路权值为1,最后 floyed 算法计算最短路。

#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=500;
int mp[maxn][maxn];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    while(m--)
    {
        int x,y;
        scanf(" %d%d",&x,&y);
        mp[x][y]=mp[y][x]=1;
    }
    if(mp[1][n])
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(mp[i][j])
                    mp[i][j]=INF;
                else
                    mp[i][j]=1;
            }
        }
    }
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(!mp[i][j])
                mp[i][j]=INF;
                
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j && j!=k)
                mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
    if(mp[1][n]<INF) printf("%d\n",mp[1][n]);
    else puts("-1");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幸愉聊信奥

谢谢亲的支持,我会继续努力啦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值