• 四川第七届 I Travel(bfs)


    Travel

    The country frog lives in has nn towns which are conveniently numbered by 1,2,,n1,2,…,n.

    Among n(n1)2n(n−1)2 pairs of towns, mm of them are connected by bidirectional highway, which needs aa minutes to travel. The other pairs are connected by railway, which needs bb minutes to travel.

    Find the minimum time to travel from town 11 to town nn.

    Input

    The input consists of multiple tests. For each test:

    The first line contains 44 integers n,m,a,bn,m,a,b (2n105,0m5105,1a,b1092≤n≤105,0≤m≤5⋅105,1≤a,b≤109). Each of the following mmlines contains 22 integers ui,viui,vi, which denotes cities uiui and vivi are connected by highway. (1ui,vin,uivi1≤ui,vi≤n,ui≠vi).

    Output

    For each test, write 11 integer which denotes the minimum time.

    Sample Input

        3 2 1 3
        1 2
        2 3
        3 2 2 3
        1 2
        2 3

    Sample Output

        2
        3

    题意:有n个城市,编号为1~n,每个城市都相互连通,其中有m对城市通过公路连通,其他的城市通过铁路连通,经过公路的时间为a,
    经过铁路的时间为b,问从1到达n的时间最短为多少.
    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<vector>
    #define  ll long long
    using namespace std;
    int n,m;
    ll a,b;
    vector<int>v[100005];
    struct node
    {
        int v;
        ll t;
    };
    queue<node>q;
    bool bo[100005];
    ll min1(ll a,ll b)
    {
        if(a>b)return b;
        return a;
    }
    int main()
    {
        while(~scanf("%d %d %lld %lld",&n,&m,&a,&b))
        {
            while(!q.empty()) q.pop();
            for(ll i=0;i<100005;i++) v[i].clear();
            memset(bo,0,sizeof(bo));
            bool bb=0;
            for(ll i=1;i<=m;i++)
            {
                int x,y;
                scanf("%d %d",&x,&y);
                v[x].push_back(y);
                v[y].push_back(x);
                if((x==1&&y==n)||(x==n&&y==1))bb=1;//可能高速比公路慢
            }
            if(a>=b)
            {//挑出与n没有高速公路的点中存不存在与1也没有高速公路的点
                if(bb==0)printf("%d
    ",b);
                else
                {
                    for(int i=2;i<n;i++)
                    {
                        bool is=0;
                        for(int j=0;j<v[i].size();j++)
                        {
                            if(v[i][j]==1||v[i][j]==n)is=1;
                        }
                        if(is==0)
                        {
                            bb=0;break;
                        }
                    }
                    if(bb==0)printf("%d
    ",min(a,2*b));
                    else printf("%d
    ",a);
                }
                continue;
            }
            node s;
            s.v=1;
            s.t=0;
            q.push(s);
            ll mx=b;
            bo[1]=1;
            bool f=0;
            while(!q.empty())
            {
                node s;
                s=q.front();
                q.pop();
                for(int i=0;i<v[s.v].size();i++)
                {
                    int y=v[s.v].at(i);
                    if(bo[y]) continue;
                    if(y==n)
                    {
                        mx=min1(mx,(s.t+1)*a);
                        f=1;
                        break;
                    }
                    node ss;
                    ss.t=s.t+1;
                    ss.v=y;
                    q.push(ss);
                }
                if(f)break;
            }
            printf("%lld
    ",mx);
        }
        return 0;
    }


  • 相关阅读:
    Redis常用配置说明
    Redis入门知识
    分布式理论基石CAP理论
    MySQL之视图
    MySQL之事务控制总结
    MySQL之标识列(自增长列)设置起始值与步长
    LeetCode 543. Diameter of Binary Tree(两节点最长路径)
    LeetCode 110. Balanced Binary Tree(平衡树)
    LeetCode 104. Maximum Depth of Binary Tree(求树的高度)
    LeetCode 328. Odd Even Linked List(链表元素按奇偶聚集)
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271041.html
Copyright © 2020-2023  润新知