题意:

求 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. SVN经常使用命令总结(持续更新)

    如今流行的协同管理工具预计就属SVN和Git了.这两者都使用过,只是如今正在使用的是SVN.故将常常使用的命令总结下来. 无论是Windows端的svnclient还是eclipse的subversi ...

  2. EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器

    在不断进行EasyDarwin开源流媒体服务器的功能和性能完善的同时,我们也配套实现了目前在安防和移动互联网行业比较火热的移动端手机直播方案,主要就是我们的 EasyPusher直播推送项目 和 Ea ...

  3. java四种线程池简介,使用

    为什么使用线程池 1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务. 2.可以根据系统的承受能力,调整线程池中工作线线程的数目,防止消耗过多的内存 3.web项目应该创建统 ...

  4. httpclient4 模拟访问网页 模拟登录 简单例子

    JAVA后台模拟登录一个网站,获得一定权限后进一步操作. 所用的工具: Apache HttpComponents client 4.3版本 以下为代码: import org.apache.http ...

  5. ElasticSearch(三)mac安装

    1.首先要安装jdk 2.到官网或是用brew下载ElasticSearch 安装包,这边我们选择在官网下载对应的安装包 https://www.elastic.co/cn/downloads/ela ...

  6. vim学习之以退为进——可反复移动和可反复改动的美妙结合

    时间:2014.06.29 地点:基地 -------------------------------------------------------------------------------- ...

  7. shapes

    接口 shape package shape; public abstract interface shape { public abstract void Draw(); public abstra ...

  8. 后台管理微服务(二)——docker的使用

    1. docker概述 1.1 Docker是什么 Docker 是软件工业的集装箱技术 Docker 是一个容器引擎,docker提供了一套完整的容器解决方案. Docker 是一个能将开发的程序自 ...

  9. 【React系列】Props 验证

    Props 验证使用 propTypes,它可以保证我们的应用组件被正确使用,React.PropTypes 提供很多验证器 (validator) 来验证传入数据是否有效.当向 props 传入无效 ...

  10. jquery中的attr与prop

    http://www.cnblogs.com/Showshare/p/different-between-attr-and-prop.html