题意:

求 1-N 的第二长路,一条路可以重复走

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

思路:

一开始想的就是:

我只要在spfa中更新的时候记录 dis[1][i]的最小和次小就好啦;

其实每个权值都带一个附属权值就好了;

这样能解决的好少,光是重复就不行了;

正确思路:

思想类似dijkstra 算法里:每次取最小边然后更新一波,只不过这里最小边有两种罢了;

次短路要么从某个最短路过来,要么从某个次短路过来。对于从1过去的点,只要有被更新到最短路或者次短路,那么就要对所有的点都搞一遍,每次取最小的边,然后一直更新;
引我南哥的小代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <deque>
#include <map>
#define cler(arr, val) memset(arr, val, sizeof(arr))
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long LL;
const int MAXN = 5010+1;
const int MAXM = 240000;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
int d[MAXN][2],head[MAXN],tol;
bool vis[MAXN][2];
struct node
{
int u,v,val,next;
}edge[MAXM];
void addedge(int u,int v,int w)
{
edge[tol].u=u,edge[tol].v=v,edge[tol].val=w,edge[tol].next=head[u];
head[u]=tol++;
edge[tol].u=v,edge[tol].v=u,edge[tol].val=w,edge[tol].next=head[v];
head[v]=tol++;
}
void dij(int n)
{
for(int i=0;i<=n;i++)
d[i][0]=d[i][1]=INF,vis[i][0]=vis[i][1]=false;
d[0][0]=0;
while(true)
{
int v=-1,k;
int minlen=INF;
for(int u=0;u<n;u++)
{
for(int l=0;l<2;l++)
if(!vis[u][l]&&(v==-1||d[u][l]<minlen))
{
minlen=d[u][l];
k=l;
v=u;
}
}
if(v==-1) break;
vis[v][k]=true;
for(int i=head[v];~i;i=edge[i].next)
{
int u=edge[i].v;//目标
int cost=d[v][k]+edge[i].val;
if(cost<d[u][0])
{
d[u][1]=d[u][0];
d[u][0]=cost;
}
else if(cost<d[u][1]&&cost>d[u][0])
{
d[u][1]=cost;
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
int t,n,m,u,v,w,cas=1;
cin>>t;
while(t--)
{
cler(head,-1);
tol=0;
cin>>n>>m;
int a=0;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
a=max(a,u);
a=max(a,v);
u--,v--;
addedge(u,v,w);
}
dij(a);
printf("Case %d: %d\n",cas++,d[n-1][1]);
}
return 0;
}

lightoj 1099【dijkstra/BFS】的更多相关文章

  1. HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

    Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  2. Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】

    Help Hanzo (LightOJ - 1197) [简单数论][筛区间质数] 标签: 入门讲座题解 数论 题目描述 Amakusa, the evil spiritual leader has ...

  3. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. 【openjudge】【搜索(bfs)】P4980拯救行动

    [描述:] 公主被恶人抓走,被关押在牢房的某个地方.牢房用N*M (N, M <= 200)的矩阵来表示.矩阵中的每项可以代表道路(@).墙壁(#).和守卫(x). 英勇的骑士(r)决定孤身一人 ...

  5. CodeVS 1226 倒水问题【DFS/BFS】

    题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水 ...

  6. lightoj 1025【区间DP】

    题意: 给出一个word,求有多少种方法你从这个word清除一些字符而达到一个回文串. 思路: 区间问题,还是区间DP: 我判断小的区间有多少,然后往外扩大一点. dp[i,j]就代表从i到j的方案数 ...

  7. CDOJ 1964 命运石之门【最短路径Dijkstra/BFS】

    给定数字n,m(1<=n,m<=500000) 将n变为n*2花费2,将n变为n-3花费3,要求过程中所有数字都在[1,500000]区间内. 求将n变为m的最少花费 思路:建图 将每个数 ...

  8. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  9. HDU 2102 A计划【三维BFS】

    A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

随机推荐

  1. java nio 通道(二)

    本文章来源于我的个人博客: java nio 通道(二) 一,文件通道 文件通道总是堵塞式的,因此不能被置于非堵塞模式. FileChannel对象是线程安全的.多个进程能够在同一个实例上并发调用方法 ...

  2. Spring整合Hibernate的方法

    一.基本支持 Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. Spring 对这些 ORM 框架的支持是一致的, 因此 ...

  3. c# 怎么获取自己的IP地址

    1.aspx页面,asp.net项目的页面 <%@ Page Language="C#" AutoEventWireup="true" CodeBehin ...

  4. 5 Ways to Make Your Hive Queries Run Faster

    5 Ways to Make Your Hive Queries Run Faster Technique #1: Use Tez  Hive can use the Apache Tez execu ...

  5. Kotlin基本语法笔记2之类型检测及自动类型转换、循环

    类型检测及自动类型转换 is运算符用于检测一个表达式是否为某类型的一个实例检测出为某类型后,检测后的分支中可以直接当作该类型使用,无需显示转换 fun getStringLength(obj: Any ...

  6. C++正则表达式笔记之wregex

    遍历所有匹配 #include <iostream> #include <regex> using namespace std; int main() { wstring ws ...

  7. [haoi2014]贴海报

    Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委员会为选民准备了一个张贴海报的electoral墙.张贴规则如下:1.electoral墙是 ...

  8. The import ....cannot be resolved 解决方法

    1:右击项目build path>configure build path>libraries看有没感叹号什么的不正常的lib,移除掉 2:点击项目的build path>confi ...

  9. Protobuf入门实例

    Protobuf是一个灵活.高效.结构化的数据序列化框架,相比于XML等传统的序列化工具, 它更小.更快.更简单.Protobuf支持数据结构化一次就可以到处使用,甚至是跨语言使用,通过代码生成工具可 ...

  10. ubuntu熟悉过程中遇到一些小问题记录一下

    0. 使用su命令时提示:  authentication failur 安装ubuntu系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在终端中输入如下命令:sudo p ...