• ACM学习历程—CodeForces 601A The Two Routes(最短路)


    题目链接:http://codeforces.com/problemset/problem/601/A

    题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇。

    此外,有铁路的地方肯定没有陆路。

    这种情况下就会有一个结论,就是至少有一种交通可以直接1n

    这样只需要对另一种跑一个最短路就OK了。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #define LL long long
    
    using namespace std;
    
    const int maxN = 405;
    int n, m;
    bool dis[maxN][maxN];
    int p[maxN];
    bool flag;
    struct node
    {
        int val;
        int id;
        bool operator<(node x) const
        {
            return val > x.val;
        }
    };
    
    void input()
    {
        memset(dis, false, sizeof(dis));
        int u, v;
        for (int i = 0; i < m; ++i)
        {
            scanf("%d%d", &u, &v);
            dis[u][v] = dis[v][u] = true;
        }
        if (dis[1][n]) flag = false;
        else flag = true;
    }
    
    void work()
    {
        memset(p, -1, sizeof(p));
        int ans = 1, u;
        node k, v;
        priority_queue<node> q;
        p[1] = 0;
        k.val = 0;
        k.id = 1;
        q.push(k);
        while (!q.empty())
        {
            k = q.top();
            q.pop();
            u = k.id;
            for (int i = 1; i <= n; ++i)
            {
                if (i == u) continue;
                if (dis[i][u] != flag) continue;
                if (p[i] == -1 || p[i] > p[u]+1)
                {
                    p[i] = p[u]+1;
                    k.id = i;
                    k.val = p[i];
                    q.push(k);
                }
            }
        }
        if (p[n] == -1) ans = -1;
        else ans = p[n];
        printf("%d
    ", ans);
    }
    
    int main()
    {
        //freopen("test.in", "r", stdin);
        while (scanf("%d%d", &n, &m) != EOF)
        {
            input();
            work();
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Java 跨系统开发隐患(一)
    SpringBoot邮件推送功能
    基于图灵api的Python机器人
    JSP编码问题解决方法
    记一次数据结构课设
    基于百度语音识别API的Python语音识别小程序
    帝国cms过滤采集内容
    如何批量取消文章审核
    评论时判断会员是否登录
    帝国cms把文章加入到收藏夹代码
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/5027151.html
Copyright © 2020-2023  润新知