• codeves 1021


    题目描述 Description

    麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复。

        因为她和他们不住在同一个城市,因此她开始准备她的长途旅行。

        在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城市路上所需花费的时间。

        麦克在车中无意中听到有一条路正在维修,并且那儿正堵车,但没听清楚到底是哪一条路。无论哪一条路正在维修,从玛丽卡所在的城市都能到达麦克所在的城市。

        玛丽卡将只从不堵车的路上通过,并且她将按最短路线行车。麦克希望知道在最糟糕的情况下玛丽卡到达他所在的城市需要多长时间,这样他就能保证他的女朋友离开该城市足够远。

    编写程序,帮助麦克找出玛丽卡按最短路线通过不堵车道路到达他所在城市所需的最长时间(用分钟表示)。

    输入描述 Input Description

    第一行有两个用空格隔开的数N和M,分别表示城市的数量以及城市间道路的数量。1≤N≤1000,1≤M≤N*(N-1)/2。城市用数字1至N标识,麦克在城市1中,玛丽卡在城市N中。

    接下来的M行中每行包含三个用空格隔开的数A,B和V。其中1≤A,B≤N,1≤V≤1000。这些数字表示在A和城市B中间有一条双行道,并且在V分钟内是就能通过。

    输出描述 Output Description

       输出文件的第一行中写出用分钟表示的最长时间,在这段时间中,无论哪条路在堵车,玛丽卡应该能够到达麦克处,如果少于这个时间的话,则必定存在一条路,该条路一旦堵车,玛丽卡就不能够赶到麦克处。

    样例输入 Sample Input

    5 7

    1 2 8

    1 4 10

    2 3 9

    2 4 10

    2 5 1

    3 4 7

    3 5 10

    样例输出 Sample Output

    27

    思路:我们肯定想得到是将每一条路依次去掉,然后跑最短路求得最大值,然而我们发现我们去掉的肯定是最短路上面的那些边,所以我们可以将这些边标记起来.

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int Max=1002;
     4 const int INF=1e8;
     5 
     6 int n,p,c;//牛,牧场,道路//
     7 int a[Max],last[Max];
     8 struct node{
     9     int from,to,next,val;
    10 }e[Max*Max];
    11 int len,vis[Max],dis[Max];
    12 
    13 void add(int u,int v,int x){
    14     e[len].from=u;
    15     e[len].to=v;e[len].next=last[u];
    16     e[len].val=x;last[u]=len++;
    17 }
    18 struct point
    19 {
    20     int val,id;
    21     point(int id,int val):id(id),val(val){}
    22     bool operator <(const point &x)const{
    23         return val>x.val;
    24     }
    25 };
    26 
    27 int fa[Max],del[Max*Max];
    28 void hh(bool k)
    29 {
    30     memset(vis,0,sizeof(vis));
    31     memset(dis,127,sizeof(dis));
    32     priority_queue<point> q;
    33     q.push(point(1,0));
    34     dis[1]=0;
    35     while(!q.empty())
    36     {
    37         int cur=q.top().id;
    38         int curr=q.top().val;
    39         q.pop();
    40         if(vis[cur]) continue;
    41         vis[cur]=true;
    42         for(int i=last[cur];i!=-1;i=e[i].next)
    43         {
    44             int id=e[i].to;
    45             if(!del[i]&&!vis[id] && curr+e[i].val < dis[id])
    46             {
    47                 if(k) fa[id]=i;
    48                 dis[id]=curr+e[i].val;
    49                 q.push(point(id,dis[id]));
    50             }
    51         }
    52     }
    53 }
    54 int main()
    55 {
    56     int x,y,z;
    57     scanf("%d%d",&n,&p);
    58     memset(last,-1,sizeof(last));
    59     for(int i=1;i<=p;i++){
    60         scanf("%d%d%d",&x,&y,&z);
    61         add(x,y,z);
    62         add(y,x,z);
    63     }
    64     hh(true);
    65     int ans=dis[n];
    66     for(int i=n;i!=1;i=e[fa[i]].from){
    67         del[fa[i]]=1;hh(false);del[fa[i]]=0;
    68         ans=max(ans,dis[n]);
    69     }
    70     cout<<ans<<endl;
    71     return 0;
    72 }
  • 相关阅读:
    51nod 1416 两点 dfs
    Codeforces Round #424 (Div. 2) A-C
    Codeforces Round #423 (Div. 2) A-C
    Codeforces Round #422 (Div. 2) A-C
    HDU 6077 Time To Get Up 模拟
    51nod 1381 硬币游戏 概率
    51nod 1100 斜率最大 计算几何
    hihocoder 1287 : 数论一·Miller-Rabin质数测试 大质数判定
    字典树
    数论
  • 原文地址:https://www.cnblogs.com/hhxj/p/7003460.html
Copyright © 2020-2023  润新知