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 ...
随机推荐
- Linux部署之批量自动安装系统之Kickstart篇
1. 安装 2. 在桌面环境下啊配置 3. Kickstart之基本配置 4. Kickstart之安装方法 5. ...
- HDU 1556 Color the ball【树状数组】
题意:给出n个区间,每次给这个区间里面的数加1,询问单点的值 一维的区间更新,单点查询,还是那篇论文里面讲了的 #include<iostream> #include<cstdio& ...
- css文字超出变省略号...
<style>.text1 { width:200px; overflow:hidden; text-overflow:ellipsis; -o-text-over ...
- CSS3———linear-gradient() 线性渐变
线性渐变linear-gradient() 遇到了这样的css样式 body { height: 100%; background-color: #ffffff; background-image: ...
- Git常见问题 资料汇总
来源https://blog.csdn.net/albb_/article/details/80420468
- (四)React高级内容
1. React developertools安装及使用 2. PropTypes与DefaultProps 讲一下PropTypes, 先拿TodoItem来说: 从几种类型中选: 3 props ...
- POJ 1742 Coins(多重背包?)
题解 一个自然的思路是对于每一个物品做一次01背包 然后T飞了. 试着用二进制拆分,还是T了. 单调队列,对不起,懒,不想写. 我们这样想.设dp[i]代表i这个面值前几种硬币是否能凑到 然后对于每一 ...
- apache源码编译安装
源码安装apche 下载apache的源码包文件 访问http://mirror.bit.edu.cn/apache/httpd/,复制如下gz文件的链接地址,并使用wget下载到本地 wget -P ...
- Object-C,数组NSArray
晚上回来,写了2个iOS应用程序. 就是在界面中,展示标签.一种是手动构造界面,然后绑定事件.另外一种是,使用自带的界面作为容器,但是手动向里面放其它界面元素. 书中的观点是,使用图形化界面,构造界面 ...
- ztree实现根节点单击事件,显示节点信息
这段时间在维护公司的项目,去年做的项目里面有ztree树的例子,想起之前还没有开始写博客,一些知识点也无从找起,要新加一个右击节点事件,折腾了半天,其中也包含了一些知识点,稍稍做了一些demo. zT ...