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 }

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

时间: 2024-08-30 03:24:26

hdu--1595-另类最短路的相关文章

hdu 1595 最短路

题意是要我们求出最短路中一条路坏的情况下最大的时间: 我们先将最短路求出并记录路径,然后枚举最短路中每一条路坏的情况,求出最大的时间.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<cmath> using namespace st

HDU 2544:最短路( 最短路径入门 &amp;&amp;Dijkstra &amp;&amp; floyd )

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 30972    Accepted Submission(s): 13345 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

hdu 2112 HDU Today (最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给出起点和终点,然后算出最短的路. 不过有好多细节要注意: (1)起始点和终止点相等的时候,这里注意不能直接输出0,必须用标记,因为数据可能还没有处理完!!!此处贡献n次wa. (2)这里是某大神教我的用map进行转换,将字符串转换成数字,值得参考.map<string,int>M,V:不过这里同样是wa了好多次.要注意的是将最先输入的开始和结束的点也要放到这个map里面. (3)

hdu 4568 Hunter 最短路+dp

Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2014    Accepted Submission(s): 615 Problem Description One day, a hunter named James went to a mysterious area to find the treasures. Jame

hdu2112(HDU Today 简单最短路)

Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了.这样住了一段时间,徐总对当地的交通还是不太了解.有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格).徐总经常会问蹩脚的英文问路:“Can

hdu oj 2544 最短路(最短路径)

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31874    Accepted Submission(s): 13798 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

hdu 4568 spfa 最短路算法+旅行商问题

http://acm.hdu.edu.cn/showproblem.php?pid=4568 Problem Description One day, a hunter named James went to a mysterious area to find the treasures. James wanted to research the area and brought all treasures that he could. The area can be represented a

hdu 1595 最短路算法dijkstra

find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2094    Accepted Submission(s): 739 Problem Description Marica is very angry with Mirko because he found a new gi

hdu 1595 find the longest of the shortest(dijstra + 枚举)

http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图,让输出从中删除任意一条边后所得最短路径中最长的.. 思路: 直接枚举每条边想必是不行的.其实有些边是不需要枚举的,因为删除它们并不影响起点到终点的最短路.起作用的边都是未删边前的最短路径上的边,删除它们最短距离肯定增大,只需在这些最短距离中求最大的即可. 记录最短路径上的边,只需一个pre数组记录松弛时每个点的前驱节点. #include <stdio.h> #include &

HDU 5521.Meeting 最短路模板题

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3361    Accepted Submission(s): 1073 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo