POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks
http://poj.org/problem?id=3255
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15680 | Accepted: 5510 |
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 intersectionN.
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
Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
Output
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Source
#include <algorithm>
#include <cstdio>
#include <queue> using namespace std; const int INF(0x3f3f3f3f);
const int N(+);
const int M(+); int hed[N],had[N],sumedge;
struct Edge
{
int v,next,w;
}edge1[M],edge2[M];
inline void ins(int u,int v,int w)
{
edge1[++sumedge].v=v;
edge1[sumedge].next=hed[u];
edge1[sumedge].w=w;
hed[u]=sumedge;
edge2[sumedge].v=u;
edge2[sumedge].next=had[v];
edge2[sumedge].w=w;
had[v]=sumedge; edge1[++sumedge].v=u;
edge1[sumedge].next=hed[v];
edge1[sumedge].w=w;
hed[v]=sumedge;
edge2[sumedge].v=v;
edge2[sumedge].next=had[u];
edge2[sumedge].w=w;
had[u]=sumedge;
} int dis[N];
bool inq[N];
void SPFA(int s)
{
for(int i=;i<s;i++) dis[i]=INF;
dis[s]=; inq[s]=;
queue<int>que; que.push(s);
for(int u,v;!que.empty();)
{
u=que.front(); que.pop(); inq[u]=;
for(int i=had[u];i;i=edge2[i].next)
{
v=edge2[i].v;
if(dis[v]>dis[u]+edge2[i].w)
{
dis[v]=dis[u]+edge2[i].w;
if(!inq[v]) que.push(v),inq[v]=;
}
}
}
} struct Node
{
int now,g;
bool operator < (Node a) const
{
return a.g+dis[a.now]<g+dis[now];
}
};
int Astar(int s,int t,int k)
{
priority_queue<Node>que;
int cnt=; Node u,v;
u.g=; u.now=s;
que.push(u);
for(;!que.empty();)
{
u=que.top(); que.pop();
if(u.now==t) cnt++;
if(cnt==k) return u.g;
for(int i=hed[u.now];i;i=edge1[i].next)
{
v.now=edge1[i].v;
v.g=u.g+edge1[i].w;
que.push(v);
}
}
return ;
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
// freopen("block.in","r",stdin);
// freopen("block.out","w",stdout); int n,m; read(n),read(m);
for(int v,u,w;m--;)
read(u),read(v),read(w),ins(u,v,w);
SPFA(n); printf("%d\n",Astar(,n,));
return ;
} int I_want_AC=AC();
int main(){;}
Astar AC
次短路正经做法:
SPFA跑出从1到i和从n到i的dis,枚举每条不在最短路上的边,更新ans
#include <algorithm>
#include <cstdio>
#include <queue> using namespace std; const int INF(0x3f3f3f3f);
const int N(+);
const int M(+); int m,n,head[N],sumedge;
struct Edge
{
int v,next,w;
Edge(int v=,int next=,int w=):
v(v),next(next),w(w){}
}edge[M<<];
inline void ins(int u,int v,int w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} bool inq[N];
int d1[N],d2[N];
inline void SPFA(int s,int *dis)
{
for(int i=;i<=n;i++)
inq[i]=,dis[i]=INF;
dis[s]=; inq[s]=;
queue<int>que; que.push(s);
for(int u,v;!que.empty();)
{
u=que.front(); que.pop(); inq[u]=;
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if(!inq[v]) que.push(v),inq[v]=;
}
}
}
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
freopen("block.in","r",stdin);
freopen("block.out","w",stdout); read(n),read(m);
for(int v,u,w;m--;ins(u,v,w),ins(v,u,w))
read(u),read(v),read(w);
SPFA(,d1); SPFA(n,d2);
int ans=INF,tmp;
for(int i,v,u=;u<=n;u++)
{
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
tmp=d1[u]+d2[v]+edge[i].w;
if(tmp>d1[n]&&ans>tmp) ans=tmp;
}
}
printf("%d\n",ans);
return ;
} int I_want_AC=AC();
int main(){;}
SPFA 跑次短路
堆优化的Dijkstra
用两个数组记录到当前点的最小值d1[n]和次小值d2[n],注意d2[s]=INF而不是0
#include <algorithm>
#include <cstdio>
#include <queue> using namespace std; const int INF(0x3f3f3f3f);
const int N(+);
const int M(+); int m,n,head[N],sumedge;
struct Edge
{
int v,next,w;
Edge(int v=,int next=,int w=):
v(v),next(next),w(w){}
}edge[M<<];
inline void ins(int u,int v,int w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} struct Node
{
int now,dis;
bool operator < (const Node &x) const
{
return dis>x.dis;
}
}; bool vis[N];
int d1[N],d2[N];
inline void Dijkstar()
{
for(int i=;i<=n;i++) d1[i]=d2[i]=INF;
priority_queue<Node>que; Node u,to;
u.dis=d1[]=; vis[]=;
u.now=; que.push(u);
for(int dis,v;!que.empty();)
{
u=que.top();que.pop();
if(u.dis>d2[u.now]) continue;
for(int i=head[u.now];i;i=edge[i].next)
{
v=edge[i].v;
dis=u.dis+edge[i].w;
if(dis<d1[v])
{
swap(dis,d1[v]);
to.now=v;
to.dis=d1[v];
que.push(to);
}
if(dis>d1[v]&&dis<d2[v])
{
d2[v]=dis;
to.dis=d2[v];
to.now=v;
que.push(to);
}
}
}
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
#define MINE
#ifdef MINE
freopen("block.in","r",stdin);
freopen("block.out","w",stdout);
#endif read(n),read(m);
for(int v,u,w;m--;ins(u,v,w),ins(v,u,w))
read(u),read(v),read(w);
Dijkstar();
printf("%d\n",d2[n]);
return ;
} int I_want_AC=AC();
int main(){;}
Dijkstra AC
POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks的更多相关文章
- 洛谷——P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...
- 洛谷P2865 [USACO06NOV]路障Roadblocks——次短路
给一手链接 https://www.luogu.com.cn/problem/P2865 这道题其实就是在维护最短路的时候维护一下次短路就okay了 #include<cstdio> #i ...
- 络谷 P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...
- BZOJ 1726 洛谷 2865 [USACO06NOV]路障Roadblocks【次短路】
·求1到n的严格次短路. [题解] dijktra魔改?允许多次入队,改了次短路的值也要入队. #include<cstdio> #include<algorithm> #de ...
- P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 最短路(次短路) 直接在dijkstra中维护2个数组:d1(最短路),d2(次短路),然后跑一遍就行了. attention:数据有不同 ...
- 洛谷P2865 [USACO06NOV]Roadblocks G(次短路)
一个次短路的问题,可以套用dijkstra求最短路的方法,用dis[0][i]表示最短路:dis[1][i]表示次短路,优先队列中存有最短路和次短路,然后每次找到一条道路对他进行判断,更新最短或次短路 ...
- cogs 315. [POJ3255] 地砖RoadBlocks
315. [POJ3255] 地砖RoadBlocks ★★★ 输入文件:block.in 输出文件:block.out 简单对比时间限制:1 s 内存限制:128 MB Descri ...
- P2865 【[USACO06NOV]路障Roadblocks】(次短路)
传送门 算法Dijkstra要求次短路 那么在不考虑重复走一条边的情况下 肯定是把最短路中的一段改成另一段 至少要换另一条边到路径里所以可以枚举所有不属于最短路的每条边(a,b) 那么dis(1,a) ...
- 【洛谷 P2865】 [USACO06NOV]路障Roadblocks(最短路)
题目链接 次短路模板题. 对每个点记录最短路和严格次短路,然后就是维护次值的方法了. 和这题一样. #include <cstdio> #include <queue> #in ...
随机推荐
- 联想lenovo 家用电脑安装win7 无法引导问题(新电脑安装系统时提示File:\Boot\BCD错误解决方案)
安装方式 : 1.进入 PE 2.用 EasyimageX 恢复 GHO镜像 3.重启后出现 原因: 主要 是安装win7 时,格式 化选择为GUID模式. 处理: win7 以后,格式华时选择MB ...
- Linxu基本指令
一.Linux权限的概念 Linux下有两种用户:普通用户和超级用户(). 普通用户:在linux下做有限的事情: 超级用户:可以在linux系统下做任何事情,不受限制. 普通用户的提示符是“$”,超 ...
- 简洁的MVC思想框架——Nancy(环境配置与Get操作)
Nancy官网——https://github.com/NancyFx/Nancy 概述:Nancy是一个开源的Web轻型框架内核符合MVC思想,有开发方便,路由简单的特点,而且功能齐全 起步:Hel ...
- BZOJ 2342 [Shoi2011]双倍回文(manacher+堆+set)
题意 N<=500000 题解 维护一个set可以用堆来解决. #include<iostream> #include<cstring> #include<cstd ...
- 【Paper Reading】Bayesian Face Sketch Synthesis
Contribution: 1) Systematic interpretation to existing face sketch synthesis methods. 2) Bayesian fa ...
- jQuery第四课 点击 _选项卡效果一
//鼠标移到上面是显示手型cursor:pointer jquery 的函数: siblings //兄弟节点,同胞元素 :eq()选择器选取带有指定 index 值的元素.index 值从 0 开始 ...
- UI布局【转】
转载自: https://www.cnblogs.com/wangdaijun/p/5519459.html https://www.jianshu.com/p/f781c40df57c Good U ...
- Linux split 命令用法详解 - 切割文件[转]
功能说明:切割文件.语 法:split [--help][--version][-<行数>][-b <字节>][-C <字节>][-l <行数>][要切 ...
- 数学之路-python计算实战(6)-numpy-ndarray
>>>> mya=np.zeros(shape=(2,2)) >>>> mya array([[ 0., 0.], [ 0., 0.]]) > ...
- jmeter名词解释之时间(Elapsed Time/ Latency Time/Connection Time)
转载时请标注源自:http://blog.csdn.net/musen518 jmeter报告结果中会出现三个时间 1. Elapsed time 经过的时间(= Sample time = L ...