lightoj 1099【dijkstra/BFS】
题意:
求 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 算法里:每次取最小边然后更新一波,只不过这里最小边有两种罢了;
#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】的更多相关文章
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】
Help Hanzo (LightOJ - 1197) [简单数论][筛区间质数] 标签: 入门讲座题解 数论 题目描述 Amakusa, the evil spiritual leader has ...
- 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 ...
- 【openjudge】【搜索(bfs)】P4980拯救行动
[描述:] 公主被恶人抓走,被关押在牢房的某个地方.牢房用N*M (N, M <= 200)的矩阵来表示.矩阵中的每项可以代表道路(@).墙壁(#).和守卫(x). 英勇的骑士(r)决定孤身一人 ...
- CodeVS 1226 倒水问题【DFS/BFS】
题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水 ...
- lightoj 1025【区间DP】
题意: 给出一个word,求有多少种方法你从这个word清除一些字符而达到一个回文串. 思路: 区间问题,还是区间DP: 我判断小的区间有多少,然后往外扩大一点. dp[i,j]就代表从i到j的方案数 ...
- CDOJ 1964 命运石之门【最短路径Dijkstra/BFS】
给定数字n,m(1<=n,m<=500000) 将n变为n*2花费2,将n变为n-3花费3,要求过程中所有数字都在[1,500000]区间内. 求将n变为m的最少花费 思路:建图 将每个数 ...
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- HDU 2102 A计划【三维BFS】
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
随机推荐
- Java ClassLoader详解(转载)
Java ClassLoader详解 类加载器是 Java 语言的一个创新,也是 Java 语言流行的重要原因之一.它使得 Java 类可以被动态加载到 Java 虚拟机中并执行.类加载器从 JDK ...
- C++ 中的几种初始化
前言 阅读C++教材时,想必你听过复制初始化,直接初始化,值初始化这三个概念吧.笔者本人常将其混淆,遂在此记录下它们的具体含义以便日后查阅. 复制初始化( copy-initialization ) ...
- Eclipse内存错误java heap space
Eclipse安装路径下的内存配置文件:eclipse.ini 文件末尾: -XX:MaxPermSize=256m-Xms40m-Xmx512m 其中 -Xmx512m表示最大内存,改为768或10 ...
- UIBezierPath(转)
@import url(/css/cuteeditor.css); @import url(/css/cuteeditor.css); @import url(http://i.cnblogs.com ...
- h5 移动端 关于监测切换程序到后台或息屏事件和visibilitychange的使用
需求:当我们页面上正在播放视频或者播放背景音乐时,我们屏幕自动息屏或者切换程序去看消息时,我们希望暂停视频或背景音乐,回到程序我们希望继续播放视频或播放背景音乐.小程序上提供了 onUnload返回 ...
- Codeforces Round #173 (Div. 2) E. Sausage Maximization —— 字典树 + 前缀和
题目链接:http://codeforces.com/problemset/problem/282/E E. Sausage Maximization time limit per test 2 se ...
- Android BLE 总结-源码篇(BluetoothLeAdvertiser)
在做Android BLE的应用程序时,我们发出广播数据是调用BluetoothLeAdvertiser的startAdvertising方法,如下所示: mBluetoothLeAdvertiser ...
- 在一个form表单中根据不同按钮实现多个action事件
<form id="writeForm" method="post"> <div class="write-btn-tj" ...
- css绘制三角形
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Struts2 拦截器配置及使用
在我的项目中有个需求,实现记录用户操作的系统日志,基于这个功能我首先想到的是Struts 的拦截器.配置一个全部Action都会拦截的拦截,写一个公用的服务.每当用户发送请求到Action 就记录相应 ...