博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdoj 1595 最短路中的最长路(good)
阅读量:4217 次
发布时间:2019-05-26

本文共 2404 字,大约阅读时间需要 8 分钟。

#include
#include
#include
#include
using namespace std;#define MAX 1005#define INF 9999999int pre[MAX];int d[MAX];int head[MAX];int used[MAX][MAX];//1表示有效边 0表示无效边 bool marked[MAX];int dis[MAX];struct Edge{ int from, to, w; int next; };Edge edge[MAX*MAX];int n,m;int tol;int spfa() { queue
que; for(int i = 1; i <= n; i++ ){ dis[i] = INF; marked[i] = false; } dis[1] = 0; marked[1] = true; que.push(1); while(!que.empty()){ int cur = que.front(); que.pop(); marked[cur] = false; for(int i = head[cur]; i != -1; i = edge[i].next){ int v = edge[i].to; if(dis[v] > dis[cur] + edge[i].w){ dis[v] = dis[cur] + edge[i].w; pre[v] = cur; if(!marked[v]){ que.push(v); marked[v] = true; } } } } return dis[n]; } int work() { queue
que; for(int i = 1; i <= n; i++ ){ dis[i] = INF; marked[i] = false; } dis[1] = 0; marked[1] = true; que.push(1); while(!que.empty()){ int cur = que.front(); que.pop(); marked[cur] = false;//需要 及时置为false for(int i = head[cur]; i != -1; i = edge[i].next){ int v = edge[i].to; if(used[cur][v] && dis[v] > dis[cur] + edge[i].w){//与spfa区分 当最短路上某条边阻塞失效时 的最短路 dis[v] = dis[cur] + edge[i].w; if(!marked[v]){ que.push(v); marked[v] = true; } } } } return dis[n]; } void addEdge(int from, int to, int w) { edge[tol].next = head[from]; edge[tol].from = from; edge[tol].to = to; edge[tol].w = w; head[from] = tol++; }int main() { while(scanf("%d%d",&n,&m)!=EOF){ tol = 0; memset(head,-1,sizeof(head)); memset(used,0,sizeof(used)); pre[1] = -1; int a,b,c; for(int i = 0; i < m; i++ ){ scanf("%d%d%d",&a,&b,&c); used[a][b] = 1; used[b][a] = 1; addEdge(a,b,c); addEdge(b,a,c); } int ans = spfa();//阻塞路不在最短路上时候 最短路 for(int i = n; pre[i] != -1; i = pre[i]){// 对最短路上的每一条边遍历(当当前边阻塞时候最短路) used[i][pre[i]] = 0; //找到 这其中最长的路 used[pre[i]][i] = 0; int res = work(); if(res > ans){ ans = res; } used[i][pre[i]] = 1;//模拟完当前边为阻塞路径后及时 恢复有效边 继续模拟其它最短路上的边 used[pre[i]][i] = 1; } printf("%d\n",ans); } return 0; }

注意:Runtime Error (ACCESS_VIOLATION(G++提交时候) 换为c++则直接报错

可能 原因:

runtime  error (运行时错误)就是程序运行到一半,程序就崩溃了。

比如说:
①除以零
②数组越界:int a[3]; a[10000000]=10;(考虑数组开小了
③指针越界:int * p; p=(int *)malloc(5 * sizeof(int)); *(p+1000000)=10;
④使用已经释放的空间:int * p; p=(int *)malloc(5 * sizeof(int));free(p); *p=10;
数组开得太大,超出了栈的范围,造成栈溢出:int a[100000000];

转载地址:http://llimi.baihongyu.com/

你可能感兴趣的文章
Python学习笔记——数据分析之Bokeh绘图
查看>>
Python学习笔记——数据分析之数据可视化工具实战案例:世界高峰数据可视化
查看>>
Python学习笔记——科学计算工具Numpy
查看>>
Python学习笔记——数据分析之数据分析工具Pandas
查看>>
Python学习笔记——Pygame之基础知识
查看>>
Ubuntu 18.04双系统安装教程-超详细(原系统Win7,解决安装完成后启动Ubuntu进入GLUB的问题)
查看>>
Web前端学习笔记——构建前端自动化工作流环境
查看>>
Web前端学习笔记——AngularJS入门
查看>>
Web前端学习笔记——AngularJS之过滤器、服务、路由
查看>>
Web前端学习笔记——AngularJS之TodoMVC案例
查看>>
Web前端学习笔记——AngularJS之豆瓣电影案例
查看>>
Web前端学习笔记——模块化开发
查看>>
Web前端学习笔记——VueJS基础
查看>>
Web前端学习笔记——VueJS之过滤器、生命周期、请求、动画
查看>>
Web前端学习笔记——VueJS之组件、路由
查看>>
Web前端学习笔记——HTML基础
查看>>
Web前端学习笔记——CSS基础、选择器
查看>>
Web前端学习笔记——Webpack
查看>>
Web前端学习笔记——CSS样式、外观、复合选择器
查看>>
Web前端学习笔记——CSS显示模式、特性、背景
查看>>