HDU-1595-find the longest of the shortest
2016-11-25 17:54
225 查看
ACM模版
![](http://img.blog.csdn.net/20161125174631769)
![](http://img.blog.csdn.net/20161125174642363)
最后调戏一下下javaer,如果说,存在某种题只能被其他语言AC而不能被JavaAC,那么这道题就是啦~~~
描述
题解
先dij一遍,求出0到n-1的最短路径,然后枚举每一段,求出删除该段时的最短路,最后求所有情况的最短路径的最大花费。多次dij搞搞。这里需要记住的是,对dij要进行一个改造,不能每次都动pre[](最短路路径),只有第一次求的时候才会对它下刀。最后调戏一下下javaer,如果说,存在某种题只能被其他语言AC而不能被JavaAC,那么这道题就是啦~~~
代码
#include <iostream> #include <cstring> using namespace std; /* * 单源最短路径,Dijkstra算法,邻接矩阵形式,复杂度为O(n^2) * 求出源beg到所有点的最短路径,传入图的顶点数和邻接矩阵cost[][] * 返回各点的最短路径lowcost[],路径pre[],pre[i]记录beg到i路径上的父节点,pre[beg] = -1 * 可更改路径权类型,但是权值必须为非负,下标0~n-1 */ const int MAXN = 1010; const int INF = 0x3f3f3f3f; // 表示无穷 bool vis[MAXN]; int pre[MAXN]; void Dijkstra(int cost[][MAXN], int lowcost[], int n, int beg, int flag) { if (flag) { memset(pre, -1, sizeof(pre)); } for (int i = 0; i < n; i++) { lowcost[i] = INF; vis[i] = false; } lowcost[beg] = 0; for (int j = 0; j < n; j++) { int k = -1; int min = INF; for (int i = 0; i < n; i++) { if (!vis[i] && lowcost[i] < min) { min = lowcost[i]; k = i; } } if (k == -1) { break; } vis[k] = true; for (int i = 0; i < n; i++) { if (!vis[i] && lowcost[k] + cost[k][i] < lowcost[i]) { lowcost[i] = lowcost[k] + cost[k][i]; if (flag) { pre[i] = k; // 只标记最短路上的父节点 } } } } } int cost[MAXN][MAXN]; int lowcost[MAXN]; int main(int argc, const char * argv[]) { int n, m; while (cin >> n >> m) { memset(cost, 0x3f, sizeof(cost)); int x, y, w; for (int i = 0; i < m; i++) { scanf("%d%d%d", &x, &y, &w); x--, y--; if (w < cost[x][y]) { cost[x][y] = cost[y][x] = w; } } Dijkstra(cost, lowcost, n, 0, 1); int ans = lowcost[n - 1]; for (int i = n - 1; i != 0; i = pre[i]) { int temp = cost[i][pre[i]]; cost[i][pre[i]] = cost[pre[i]][i] = INF; Dijkstra(cost, lowcost, n, 0, 0); ans = max(ans, lowcost[n - 1]); cost[i][pre[i]] = cost[pre[i]][i] = temp; } printf("%d\n", ans); } return 0; }
参考
《最短路》相关文章推荐
- HDU 1595 find the longest of the shortest
- hdu 1595 find the longest of the shortest ( spfa + 枚举 )
- HDU - 1595 find the longest of the shortest(最短路Dijkstra+枚举删边)
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest (最短路+记录路径+枚举删边)
- HDU 1595 find the longest of the shortest 删掉任意一条边的最长最短路
- HDU 1595 find the longest of the shortest【次短路】
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest(枚举,最短路)
- hdu 1595 find the longest of the shortest (枚举+最短路)
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest
- HDU 1595——find the longest of the shortest
- HDU 1595 find the longest of the shortest 删除一条边的最长最短路
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest(Dijkstra)
- hdu 1595 find the longest of the shortest (spfa)
- 【HDU】1595 find the longest of the shortest 枚举+最短路