zoukankan      html  css  js  c++  java
  • hdu--1595-另类最短路

    这题 一定要好好读题啊 不能走马观花...

    Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road

    ---有一条路是坏的 但是现在不确定究竟是那一条 就是任意的

    Mirko wants to know how long will it take for her to get to his city in the worst case,  Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.

    --这2段话 应该放一起理解 比较好 我们要选择的还是一条最短路 但是最坏的情况下.. 说白了 就是假如在所有路都连通的情况下 最短路径是

    A->B->C->D->E

    但是 现在会有一条路是closed 是不连通的 而且是那条 题目没确定 那么我们就要尝试删掉A-B / B-C / C-D / D-E

    做法 就是这样了

      1 #include <iostream>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <queue>
      5 using namespace std;
      6 
      7 int n;
      8 const int size = 1010;
      9 const int inf = 0x3f3f3f3f;
     10 queue<int>q;
     11 int dist[size];
     12 bool vis[size];
     13 int pre[size];
     14 struct data
     15 {
     16     int num;
     17     int to[size];
     18     int dist[size];
     19 }node[size];
     20 
     21 void add( int from , int to , int val )
     22 {
     23     node[from].to[ node[from].num ] = to;
     24     node[from].dist[ node[from].num ] = val;
     25     node[from].num ++;
     26 }
     27 
     28 void spfa( int flag )
     29 {
     30     int now , next;
     31     for( int i = 0 ; i<=n ; i++ )
     32     {
     33         dist[i] = (i == 1) ? 0 : inf;
     34     }
     35     memset( vis , false , sizeof(vis) );
     36     q.push(1);
     37     vis[1] = true;
     38     while( !q.empty() )
     39     {
     40         now = q.front();
     41         q.pop();
     42         vis[now] = false;
     43         for( int i = 0 ; i < node[now].num ; i++ )
     44         {
     45             next = node[now].to[i];
     46             if( dist[next] > dist[now] + node[now].dist[i] )
     47             {
     48                 dist[next] = dist[now] + node[now].dist[i];
     49                 if(flag)
     50                     pre[next] = now;
     51                 if( !vis[next] )
     52                 {
     53                     vis[next] = true;
     54                     q.push(next);
     55                 }
     56             }
     57         }
     58     }
     59 }
     60 
     61 int main()
     62 {
     63     cin.sync_with_stdio(false);
     64     int m , from , to , val , ans , index , id;
     65     while( cin >> n >> m )
     66     {
     67         memset( pre , -1 , sizeof(pre) );
     68         for( int i = 0 ; i<=n ; i++ )
     69             node[i].num = 0;
     70         for( int i = 0 ; i<m ; i++ )
     71         {
     72             cin >> from >> to >> val;
     73             add( from , to , val );
     74             add( to , from , val );
     75         }
     76         spfa(1);
     77         ans = dist[n];
     78         for( int i = n ; ~pre[i] ; i = pre[i] )
     79         {
     80             for( int j = 0 ; j<node[i].num ; j++ )
     81             {
     82                 if( node[i].to[j] == pre[i] )
     83                 {
     84                     val = node[i].dist[j];
     85                     index = j;
     86                     node[i].dist[j] = inf;
     87                     break;
     88                 }
     89             }
     90             for( int j = 0 ; j<node[ pre[i] ].num ; j++ )
     91             {
     92                 if( node[ pre[i] ].to[j] == i )
     93                 {
     94                     id = j;
     95                     node[ pre[i] ].dist[j] = inf;
     96                     break;
     97                 }
     98             }
     99             spfa(0);
    100             node[i].dist[index] = val;
    101             node[ pre[i] ].dist[id] = val;
    102             if( ans < dist[n] )
    103                 ans = dist[n];
    104         }
    105         cout << ans << endl;
    106     }
    107     return 0;
    108 }
    View Code

     突然发现 最短路 有好多变形题啊 蛮有意思的

    just follow your heart
  • 相关阅读:
    BTree B+Tree
    SpringMvc框架 解决在RESTFUL接口后加任意 “.xxx” 绕过权限的问题
    多线程基础知识---sleep和wait区别
    多线程基础知识---join方法
    Maven跳过单元测试的两种方式
    maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令
    Maven项目版本继承 – 我必须指定父版本?
    SpringMVC 零配置 无web.xml
    利用ApplicationContextAware装配Bean
    Spring Boot 读取 resource 下文件
  • 原文地址:https://www.cnblogs.com/radical/p/3961913.html
Copyright © 2011-2022 走看看