zoukankan      html  css  js  c++  java
  • CSU-1307-二分+dij

    1307: City Tour

            Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 593     Solved: 148    


    Description

    Alice想要从城市A出发到城市B,由于Alice最近比较穷(不像集训队陈兴老师是个rich second),所以只能选择做火车从A到B。不过Alice很讨厌坐火车,火车上人比较多,比较拥挤,所以Alice有很严格的要求:火车的相邻两站间的最大距离尽可能的短,这样Alice就可以在停站的时候下车休息一下。当然Alice希望整个旅途比较短。
     

    Input

    有多组测试数据。
    每组测试数据的第一行有两个整数N,M,A,B(N<=2000, M<=50000, N >=2, A,B<=N),其中N是城市的个数,M是城市间通火车的个数。
    A,B是Alice起始的城市与目的地城市,城市的标号从1开始。
    接下来的M行每行三个整数u,v,w表示从u到v和从v到u有一条铁路,距离为w, u,v<=N, w<=10000。

    Output

    对于每组测试数据输出满足Alice要求的从A到B的最短距离。

    Sample Input

    3 3 1 2
    1 2 80
    1 3 40
    2 3 50
    3 3 1 2
    1 2 90
    1 3 10
    2 3 20
    4 5 1 4
    1 2 8
    1 4 9
    1 3 10
    2 4 7
    3 4 8
    

    Sample Output

    90
    30
    15

     1 #include<cstring>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<iostream>
     6 #include<map>
     7 #include<functional>
     8 #include<queue>
     9 using namespace std;
    10 #define pii pair<int,int>
    11 #define mp make_pair
    12 #define eps 1e-6
    13 #define inf 0x3f3f3f3f
    14 struct Edge{
    15     int u,v,w,next;
    16 }e1[100010];
    17 int first1[2010];
    18 int tot1;
    19 void add1(int u,int v,int w){
    20     e1[tot1].u=u;
    21     e1[tot1].v=v;
    22     e1[tot1].w=w;
    23     e1[tot1].next=first1[u];
    24     first1[u]=tot1++;
    25 }
    26 
    27 
    28 int d1[2010];
    29 bool vis[2010];
    30 bool dij(int s,int t,int d[],Edge e[],int first[],int x)
    31 {
    32     memset(vis,0,sizeof(vis));
    33     memset(d,inf,sizeof(int)*2005);
    34     priority_queue<pii,vector<pii>,greater<pii> >q;
    35     d[s]=0;
    36     q.push(mp(0,s));
    37     while(!q.empty()){
    38         int u=q.top().second;
    39         q.pop();
    40         if(vis[u]) continue;
    41         vis[u]=1;
    42         for(int i=first[u];i+1;i=e[i].next){
    43             if(e[i].w>x) continue;
    44             if(d[e[i].v]>d[u]+e[i].w){
    45                     d[e[i].v]=d[u]+e[i].w;
    46                     q.push(mp(d[e[i].v],e[i].v));    
    47             }
    48         }
    49     }
    50     return d[t]!=inf;
    51 }
    52 int main()
    53 {
    54     
    55     int t,n,m,i,j,k,s,cas;
    56     int u,v,w;
    57     while(cin>>n>>m>>s>>t){
    58         tot1=0;
    59         memset(first1,-1,sizeof(first1));
    60         while(m--){
    61             scanf("%d%d%d",&u,&v,&w);
    62             add1(u,v,w);
    63             add1(v,u,w);
    64         }
    65         int l=1,r=10000;
    66         while(l<r){//cout<<l<<' '<<r<<endl;
    67             int mid=l+(r-l)/2;
    68             if(dij(s,t,d1,e1,first1,mid)) r=mid;
    69             else l=mid+1;
    70         }
    71         dij(s,t,d1,e1,first1,l);
    72         cout<<d1[t]<<endl;
    73     }
    74     return 0;    
    75 } 
  • 相关阅读:
    多态的使用
    抽象类与具体类
    对象应该长什么样子
    方法的重载overload
    遵守合约:覆盖的规则
    Android 自定义Dialog
    less 之Extend 及 Extend all用法
    github常见错误整理!
    js获取元素宽高
    解决 Error: Access denied for user 'root'@'localhost' (using password: YES)
  • 原文地址:https://www.cnblogs.com/zzqc/p/8821138.html
Copyright © 2011-2022 走看看