博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 6181 Two Paths【次短路】【模板题】
阅读量:5937 次
发布时间:2019-06-19

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

Two Paths

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 236    Accepted Submission(s): 138


Problem Description
You are given a undirected graph with n nodes (numbered from 1 to n) and m edges. Alice and Bob are now trying to play a game. 
Both of them will take different route from 1 to n (not necessary simple).
Alice always moves first and she is so clever that take one of the shortest path from 1 to n.
Now is the Bob's turn. Help Bob to take possible shortest route from 1 to n.
There's neither multiple edges nor self-loops.
Two paths S and T are considered different if and only if there is an integer i, so that the i-th edge of S is not the same as the i-th edge of T or one of them doesn't exist.
 

Input
The first line of input contains an integer T(1 <= T <= 15), the number of test cases.
The first line of each test case contains 2 integers n, m (2 <= n, m <= 100000), number of nodes and number of edges. Each of the next m lines contains 3 integers a, b, w (1 <= a, b <= n, 1 <= w <= 1000000000), this means that there's an edge between node a and node b and its length is w.
It is guaranteed that there is at least one path from 1 to n.
Sum of n over all test cases is less than 250000 and sum of m over all test cases is less than 350000.
 

Output
For each test case print length of valid shortest path in one line.
 

Sample Input
 
2 3 3 1 2 1 2 3 4 1 3 3 2 1 1 2 1
 

Sample Output
 
5 3
Hint
For testcase 1, Alice take path 1 - 3 and its length is 3, and then Bob will take path 1 - 2 - 3 and its length is 5. For testcase 2, Bob will take route 1 - 2 - 1 - 2 and its length is 3
 

Source

套一个次短路模板即可

#include 
#define INF 1e16+100#define ms(x,y) memset(x,y,sizeof(x))using namespace std;typedef long long ll;typedef pair
P;const double pi = acos(-1.0);const int mod = 1e9 + 7;const int maxn = 1e5 + 5;struct Edge{ ll to,cost;};ll n,m;vector
a[maxn];ll dist[maxn],dist2[maxn];void addedge(ll u,ll v,ll w){ a[u].push_back(Edge{v,w}); a[v].push_back(Edge{u,w});}void solve(){ priority_queue
, greater

>que; //ms(dist,INF); //ms(dist2,INF); fill(dist,dist+n,INF); fill(dist2,dist2+n,INF); dist[0]=0; que.push(P(0,0)); while(que.size()) { P u=que.top();que.pop(); int v=u.second; ll d=u.first; if(dist2[v]

d2) //更新最短 { swap(dist[e.to],d2); que.push(P(dist[e.to],e.to)); } if(dist2[e.to]>d2&&dist[e.to]

转载于:https://www.cnblogs.com/Archger/p/8451561.html

你可能感兴趣的文章
ORACLE 日志管理框架 quest error manager
查看>>
linux日志服务器搭建
查看>>
bootstrap-导航条中的按钮、文本和链接
查看>>
django登陆验证装饰器
查看>>
Puppet module命令参数介绍(六)
查看>>
VB中CHR()码值对应表
查看>>
LAMP基于不同主机和fcgi的通信方式
查看>>
2013美赛建模算法关键词
查看>>
Android-Parcelable理解与使用(对象序列化)
查看>>
数据结构----图(邻接表用法)
查看>>
批量扫描雏形之在Java中调用nmap进行主机探测
查看>>
php 二维数组按照value分组
查看>>
[Linux] 文件系统
查看>>
windows 7 防火墙无法开启!错误代码5 错误代码0x6D9 解决办法
查看>>
winhex脚本
查看>>
我的友情链接
查看>>
SVN命令详解
查看>>
Windows的资源监视器
查看>>
Android图形解锁的绘制
查看>>
UML基础系列:类图
查看>>