zoukankan      html  css  js  c++  java
  • Travel(最短路)

    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 mm lines contains 22integers 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 , m, a, b ,其中 n 代表点数,并且是个完全图,m 是权值为 a 的边数,其余的边权值为 b ,问 1--n 的最短路

    题解:如果 1 和 n 之间连边为 a 那么答案一定为 a 与一条最短的全由b组成的路径的较小者,如果 1 和 n 之间连边为b,那么答案一定 

    为b和一条最短的全由a组成的路径的较小者。对于第1种情况直接bfs就可以,第二种情况由于边数较多,不能直接bfs 

    从1开始搜索与其相连的边权为b的边,用set维护一下,由于每个点只入队1次,复杂度算是 nlogn ,叉姐的题很有意思

    300ms

      1 # include <cstdio>
      2 # include <cstring>
      3 # include <cstdlib>
      4 # include <iostream>
      5 # include <vector>
      6 # include <queue>
      7 # include <stack>
      8 # include <map>
      9 # include <bitset>
     10 # include <sstream>
     11 # include <set>
     12 # include <cmath>
     13 # include <algorithm>
     14 # pragma  comment(linker,"/STACK:102400000,102400000")
     15 using namespace std;
     16 # define LL          long long
     17 # define pr          pair
     18 # define mkp         make_pair
     19 # define lowbit(x)   ((x)&(-x))
     20 # define PI          acos(-1.0)
     21 # define INF         0x3f3f3f3f3f3f3f3f
     22 # define eps         1e-8
     23 # define MOD         1000000007
     24 
     25 inline int scan() {
     26     int x=0,f=1; char ch=getchar();
     27     while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
     28     while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
     29     return x*f;
     30 }
     31 inline void Out(int a) {
     32     if(a<0) {putchar('-'); a=-a;}
     33     if(a>=10) Out(a/10);
     34     putchar(a%10+'0');
     35 }
     36 # define MX 100005
     37 /**************************/
     38 struct Edge
     39 {
     40     int v,nex;
     41 }edge[MX*10];
     42 
     43 int n,m,a,b,ip;
     44 int hlist[MX];
     45 LL dis[MX];
     46 bool vis[MX];
     47 void addedge(int u,int v)
     48 {
     49     edge[ip]= (Edge){v,hlist[u]};
     50     hlist[u]=ip++;
     51     edge[ip]= (Edge){u,hlist[v]};
     52     hlist[v]=ip++;
     53 }
     54 
     55 void bfsB() // 1-n 连b边
     56 {
     57     dis[n]=INF;
     58     memset(vis,0,sizeof(vis));
     59     queue<int> Q;
     60     Q.push(1);
     61     dis[1]=0;
     62     vis[1]=1;
     63     while (!Q.empty())
     64     {
     65         int u = Q.front(); Q.pop();
     66         for (int i=hlist[u];i!=-1;i=edge[i].nex)
     67         {
     68             int v = edge[i].v;
     69             if (!vis[v])
     70             {
     71                 dis[v]=dis[u]+1;
     72                 Q.push(v);
     73                 vis[v]=1;
     74             }
     75         }
     76         if (dis[n]!=INF) break;
     77     }
     78     printf("%lld
    ",min(dis[n]*a,(LL)b));
     79 }
     80 
     81 void bfsA() //1-n 连 a 边
     82 {
     83     dis[n]=INF;
     84     set<int> st,ts;
     85     for (int i=2;i<=n;i++) st.insert(i);
     86     set<int>::iterator it;
     87     queue<int> Q;
     88     Q.push(1);
     89     dis[1]=0;
     90     while (!Q.empty())
     91     {
     92         int u = Q.front(); Q.pop();
     93         for (int i=hlist[u];i!=-1;i=edge[i].nex)
     94         {
     95             int v=edge[i].v;
     96             if (st.count(v)==0) continue;
     97             st.erase(v); ts.insert(v);
     98         }
     99         for (it=st.begin();it!=st.end();it++)
    100         {
    101             dis[*it] = dis[u]+1;
    102             Q.push(*it);
    103         }
    104         if (dis[n]!=INF) break;
    105         st.swap(ts);
    106         ts.clear();
    107     }
    108     printf("%lld
    ",min(dis[n]*b,(LL)a));
    109 }
    110 
    111 
    112 int main()
    113 {
    114     while(scanf("%d%d%d%d",&n,&m,&a,&b)!=EOF)
    115     {
    116         memset(hlist,-1,sizeof(hlist));
    117         ip=0;
    118         bool flag=0;
    119         for (int i=0;i<m;i++)
    120         {
    121             int u = scan();
    122             int v = scan();
    123             addedge(u,v);
    124             if (u>v) swap(u,v);
    125             if (u==1&&v==n) flag=1;
    126         }
    127         if (flag)
    128         {
    129             if (a<b) printf("%d
    ",a);
    130             else bfsA();
    131         }
    132         else
    133         {
    134             if (b<a) printf("%d
    ",b);
    135             else bfsB();
    136         }
    137     }
    138     return 0;
    139 }
    View Code
  • 相关阅读:
    PMP:7.项目成本管理
    PMP:5.项目范围管理
    PMP:4.项目整合管理
    PMP:3.项目经理角色
    PMP:2.项目运行环境
    PMP:1.引论
    RxSwift学习笔记10:startWith/merge/zip/combineLatest/withLatestFrom/switchLatest
    RxSwift学习笔记9:amb/tabkeWhile/tabkeUntil/skipWhile/skipUntil
    RxSwift学习笔记8:filter/distinctUntilChanged/single/elementAt/ignoreElements/take/takeLast/skip/sample/debounce
    linux 安装rz 指令
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/7392418.html
Copyright © 2011-2022 走看看