zoukankan      html  css  js  c++  java
  • Codeforces Round #333 (Div. 1)

    A. The Two Routes

    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 town 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 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*(n-1)/2 -  m 的其余的边连上公路。而且他们走最短路是不可能相撞的。

    其实样例会误导你,公路其实,可以更短1-4.

    那么就是两边最短路。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int MAXN = 500;
    const int inf = 0x3f3f3f3f;
    
    struct Edge {
        int from,to,dist;
    };
    
    struct HeapNode {
        int d,u;
        bool operator < (const HeapNode & rhs) const {
            return d > rhs.d;
        }
    };
    
    struct Dij {
        vector<Edge> edges;
        vector<int> G[MAXN];
        int n,m;
        bool done[MAXN];
        int d[MAXN];
        int p[MAXN];
    
        void init(int n) {
            this->n = n;
            for(int i = 0; i < n; i++) G[i].clear();
            edges.clear();
        }
    
        void AddEdge (int from ,int to,int dist) {
            edges.push_back((Edge){from,to,dist});
            m = edges.size();
            G[from].push_back(m-1);
        }
    
        void dij(int s) {
            priority_queue<HeapNode> Q;
            for(int i = 0; i <n; i++) d[i] = inf;
            d[s] = 0;
            memset(done,0,sizeof(done));
            Q.push((HeapNode){0,s});
            while(!Q.empty()) {
                HeapNode x = Q.top();Q.pop();
                int u = x.u;
                if(done[u]) continue;
                done[u] = true;
    
                for(int i = 0; i <(int)G[u].size(); i++) {
                    Edge& e = edges[G[u][i]];
                    if(d[e.to] > d[u] + e.dist) {
                        d[e.to] = d[u] + e.dist;
                        p[e.to] = G[u][i];
                        Q.push((HeapNode){d[e.to],e.to});
                    }
                }
            }
        }
    
    
    }sol;
    
    bool maps[MAXN][MAXN];
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        int n,m;
        scanf("%d%d",&n,&m);
        memset(maps,0,sizeof(maps));
    
        sol.init(n);
        for(int i = 0; i < m; i++) {
            int u,v;
            scanf("%d%d",&u,&v);
            u--;v--;
            sol.AddEdge(u,v,1);
            sol.AddEdge(v,u,1);
            maps[u][v] = maps[v][u] = 1;
        }
    
        sol.dij(0);
        int ans = sol.d[n-1];
    
        sol.init(n);
        for(int i = 0; i < n; i++)
        for(int j = i+1; j < n; j++) {
            if(maps[i][j]==0) {
                sol.AddEdge(i,j,1);
                sol.AddEdge(j,i,1);
            }
        }
        sol.dij(0);
        ans = max(ans,sol.d[n-1]);
        if(ans==inf) cout<<-1<<endl;
        else cout<<ans<<endl;
    
        return 0;
    }
    View Code
  • 相关阅读:
    ViewPager 滑动页(一)
    Fragment中Button的android:onClick 无法监听相应
    Button的四种Click响应方法
    环形图 自定义(一)
    Progress 自定义(一)-shape
    Button 自定义(一)-shape
    客户机页表遍历
    KVM的ept机制
    linux内核源码中两个重要的宏
    总结
  • 原文地址:https://www.cnblogs.com/TreeDream/p/7886813.html
Copyright © 2011-2022 走看看