Roadblocks

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 6
Problem Description

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.

The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

 
Input
Line 1: Two space-separated integers: <i>N</i> and <i>R</i> <br>Lines 2..<i>R</i>+1: Each line contains three space-separated integers: <i>A</i>, <i>B</i>, and <i>D</i> that describe a road that connects intersections <i>A</i> and <i>B</i> and has length <i>D</i> (1 ≤ <i>D</i> ≤ 5000)
 
Output
Line 1: The length of the second shortest path between node 1 and node <i>N</i>
 
Sample Input
4 4 1 2 100 2 4 200 2 3 250 3 4 100
 
Sample Output
450
 
有n个路口r条马路,马路可以重复走,问从1号路口到n号路口的次短路
 

次短路问题,实际上可以这么理解:

在知道最短路的情况下,不走最短路,绕一段路,而且只能绕一段路,否则会不满足次短。

所以就先找到最短路并记录下路径,然后枚举最短路上的每一个点a,从这个点再绕一个点b,然后再加上点b到n的最短路。

所以我们需要知道从1到每个点的最短路,还需要知道从每个点到n的最短路,从每个点到n的最短路就是从n到每个点的最短路

所以两次dijkstra 然后枚举次短路就好啦

邻接矩阵居然超内存

 #include <cstring>
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define inf 0x3f3f3f3f
const int maxn = ;
using namespace std;
int n, m;
int d1[maxn];
int d2[maxn];
bool book[maxn];
struct edge
{
int to, c;
};
vector<edge> e[maxn];
//用邻接矩阵会超内存
void dijkstra(int s, int *d)
{
memset(d,inf, maxn * sizeof(int));
memset(book, , sizeof(book));
int i;
//for (i = 0; i < e[s].size(); i++) d[e[s][i].to] = e[s][i].c;
//book[s] = 1;
//不能直接赋值,也要进行比较,典型样例
//2 2
//1 2 100
//1 2 200
d[s] = ;
while ()
{
int k = -; int min = inf;
for ( i = ; i<= n;i++)
{
if (!book[i] && d[i] < min)
{
min = d[i];
k = i;
}
}
if (k == -) break; else
{
book[k] = ;
for (i=;i<e[k].size();i++)
{
if (d[e[k][i].to] > d[k] + e[k][i].c)
{
d[e[k][i].to] = d[k] + e[k][i].c;
} }
}
}
} int main()
{
int i;
cin >> n >> m;
for (i = ; i <= m; i++)
{
edge t, t1;
int k;
cin >> k >> t.to >> t.c;
t1.to = k;
t1.c = t.c;
e[k].push_back(t);//双向存。存一个点出发的多个目的地从k出发,目的地是t.to,花费t.c
e[t.to].push_back(t1);
} dijkstra(, d1);
dijkstra(n, d2);//某个点到n的最短路就是n到某个点的最短路
int k = n;
int ans = inf;
int minn = d1[n]; for (k=;k<=n;k++)
{
for (i = ; i<e[k].size(); i++)
{
int ee = d1[k] + e[k][i].c + d2[e[k][i].to];
if (ans>ee&&ee>minn)
{
ans = ee;
}
}
}
cout << ans << endl;
return ;
}

超内存代码且错误代码

 #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
int e[][];
int d1[];
int d2[];
int m, n;
void dijkstra(int s, int *d)
{
int book[];
memset(book, , sizeof(book));
int i;
for (i = ; i <= n; i++)
{
d[i] = e[s][i];
}
book[] = ;
while ()
{
int min = inf;
int k = -;
for (i = ; i <= n; i++)
{
if (d[i] < min&&book[i]==)
{
min = d[i];
k = i;
}
}
if (k == -) break;
book[k] = ;
for (i = ; i <= n; i++)
{
if (book[i] == && d[i] > d[k] + e[k][i])
{
d[i] = d[k] + e[k][i];
}
}
}
}
int main()
{
int i, j;
scanf("%d %d", &m, &n);
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
if (i == j) e[i][j] = ;
else
e[i][j] = inf;
}
}
for (i = ; i <= n; i++)
{
int x, y, z;
scanf("%d %d %d", &x, &y,&z);
if (e[x][y] > z)
{
e[x][y] = z;
e[y][x] = z;
}
}
dijkstra(, d1);
dijkstra(n, d2);
int minn = d1[n];
int ans = inf;
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
if (i == j) continue;
if (e[i][j] == inf) continue;
if (d1[i] + e[i][j] + d2[j] > minn)
{
ans = min(ans, d1[i] + e[i][j] + d2[j]);
}
}
}
printf("%d\n", ans);
return ;
}

最新文章

  1. 查询SqlServer中每张表的记录数
  2. s5pv210 cpu运行debian
  3. MySQL判断字段值来确定是否插入新记录
  4. 动态sql语句基本语法--Exec与Exec sp_executesql 的区别
  5. zookeeper适用场景:分布式锁实现
  6. (转载)关于Apache 的两种工作模式
  7. Support Facades
  8. 代码方式删除SVN
  9. ArcGIS Server 9.3 安装(win7).
  10. 身份证校验程序(下)- 零基础入门学习Delphi49
  11. Interlocked原子函数陷阱
  12. 28.Django cookie
  13. 背景重复样式background-repeat
  14. luogu 3166 组合与gcd(数三角形)结论
  15. Hive复制分区表和数据
  16. jmeter-如何在JDBC Request中添加多条语句执行
  17. pandas:对字符串类型做差分比较
  18. 1.node.js下载
  19. 最全的HTTP 响应状态码列表!
  20. (转)python+opencv实现动态物体追踪

热门文章

  1. Solr安装入门
  2. tomcat配置多个项目通过IP加端口号访问
  3. DevExpress v17.2—WPF篇(一)
  4. 2.spring 学习
  5. Android RIL的java框架
  6. GitLab项目迁移到Gerrit
  7. SWIFT中数字格式
  8. Vue2.0 分页插件pagination使用详细说明
  9. 51Nod:1134 最长递增子序列
  10. CTF之常见的两种关于word的信息隐藏技术