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

Line 1: Two space-separated integers: N and R 
Lines 2..R+1: Each line contains three space-separated integers: AB, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node N

Sample Input

4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample Output

450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

Source

USACO 2006 November Gold

mmp、、、无向图,双向边!!啊啊啊

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <queue>
  4
  5 using namespace std;
  6
  7 const int INF(0x3f3f3f3f);
  8 const int N(5000+105);
  9 const int M(200000+5);
 10
 11 int hed[N],had[N],sumedge;
 12 struct Edge
 13 {
 14     int v,next,w;
 15 }edge1[M],edge2[M];
 16 inline void ins(int u,int v,int w)
 17 {
 18     edge1[++sumedge].v=v;
 19     edge1[sumedge].next=hed[u];
 20     edge1[sumedge].w=w;
 21     hed[u]=sumedge;
 22     edge2[sumedge].v=u;
 23     edge2[sumedge].next=had[v];
 24     edge2[sumedge].w=w;
 25     had[v]=sumedge;
 26
 27     edge1[++sumedge].v=u;
 28     edge1[sumedge].next=hed[v];
 29     edge1[sumedge].w=w;
 30     hed[v]=sumedge;
 31     edge2[sumedge].v=v;
 32     edge2[sumedge].next=had[u];
 33     edge2[sumedge].w=w;
 34     had[u]=sumedge;
 35 }
 36
 37 int dis[N];
 38 bool inq[N];
 39 void SPFA(int s)
 40 {
 41     for(int i=1;i<s;i++) dis[i]=INF;
 42     dis[s]=0; inq[s]=1;
 43     queue<int>que; que.push(s);
 44     for(int u,v;!que.empty();)
 45     {
 46         u=que.front(); que.pop(); inq[u]=0;
 47         for(int i=had[u];i;i=edge2[i].next)
 48         {
 49             v=edge2[i].v;
 50             if(dis[v]>dis[u]+edge2[i].w)
 51             {
 52                 dis[v]=dis[u]+edge2[i].w;
 53                 if(!inq[v]) que.push(v),inq[v]=1;
 54             }
 55         }
 56     }
 57 }
 58
 59 struct Node
 60 {
 61     int now,g;
 62     bool operator < (Node a) const
 63     {
 64         return a.g+dis[a.now]<g+dis[now];
 65     }
 66 };
 67 int Astar(int s,int t,int k)
 68 {
 69     priority_queue<Node>que;
 70     int cnt=0;  Node u,v;
 71     u.g=0; u.now=s;
 72     que.push(u);
 73     for(;!que.empty();)
 74     {
 75         u=que.top(); que.pop();
 76         if(u.now==t) cnt++;
 77         if(cnt==k) return u.g;
 78         for(int i=hed[u.now];i;i=edge1[i].next)
 79         {
 80             v.now=edge1[i].v;
 81             v.g=u.g+edge1[i].w;
 82             que.push(v);
 83         }
 84     }
 85     return 0;
 86 }
 87
 88 inline void read(int &x)
 89 {
 90     x=0; register char ch=getchar();
 91     for(;ch>‘9‘||ch<‘0‘;) ch=getchar();
 92     for(;ch>=‘0‘&&ch<=‘9‘;ch=getchar()) x=x*10+ch-‘0‘;
 93 }
 94
 95 int AC()
 96 {
 97 //    freopen("block.in","r",stdin);
 98 //    freopen("block.out","w",stdout);
 99
100     int n,m; read(n),read(m);
101     for(int v,u,w;m--;)
102         read(u),read(v),read(w),ins(u,v,w);
103     SPFA(n);    printf("%d\n",Astar(1,n,2));
104     return 0;
105 }
106
107 int I_want_AC=AC();
108 int main(){;}

Astar AC

次短路正经做法:

SPFA跑出从1到i和从n到i的dis,枚举每条不在最短路上的边,更新ans

 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <queue>
 4
 5 using namespace std;
 6
 7 const int INF(0x3f3f3f3f);
 8 const int N(5000+105);
 9 const int M(100000+5);
10
11 int m,n,head[N],sumedge;
12 struct Edge
13 {
14     int v,next,w;
15     Edge(int v=0,int next=0,int w=0):
16         v(v),next(next),w(w){}
17 }edge[M<<1];
18 inline void ins(int u,int v,int w)
19 {
20     edge[++sumedge]=Edge(v,head[u],w);
21     head[u]=sumedge;
22 }
23
24 bool inq[N];
25 int d1[N],d2[N];
26 inline void SPFA(int s,int *dis)
27 {
28     for(int i=1;i<=n;i++)
29         inq[i]=0,dis[i]=INF;
30     dis[s]=0; inq[s]=1;
31     queue<int>que; que.push(s);
32     for(int u,v;!que.empty();)
33     {
34         u=que.front(); que.pop(); inq[u]=0;
35         for(int i=head[u];i;i=edge[i].next)
36         {
37             v=edge[i].v;
38             if(dis[v]>dis[u]+edge[i].w)
39             {
40                 dis[v]=dis[u]+edge[i].w;
41                 if(!inq[v]) que.push(v),inq[v]=1;
42             }
43         }
44     }
45 }
46
47 inline void read(int &x)
48 {
49     x=0; register char ch=getchar();
50     for(;ch>‘9‘||ch<‘0‘;) ch=getchar();
51     for(;ch>=‘0‘&&ch<=‘9‘;ch=getchar()) x=x*10+ch-‘0‘;
52 }
53
54 int AC()
55 {
56     freopen("block.in","r",stdin);
57     freopen("block.out","w",stdout);
58
59     read(n),read(m);
60     for(int v,u,w;m--;ins(u,v,w),ins(v,u,w))
61         read(u),read(v),read(w);
62     SPFA(1,d1); SPFA(n,d2);
63     int ans=INF,tmp;
64     for(int i,v,u=1;u<=n;u++)
65     {
66         for(int i=head[u];i;i=edge[i].next)
67         {
68             v=edge[i].v;
69             tmp=d1[u]+d2[v]+edge[i].w;
70             if(tmp>d1[n]&&ans>tmp) ans=tmp;
71         }
72     }
73     printf("%d\n",ans);
74     return 0;
75 }
76
77 int I_want_AC=AC();
78 int main(){;}

SPFA 跑次短路

时间: 2024-08-18 21:50:07

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 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 t

cogs 315. [POJ3255] 地砖RoadBlocks

315. [POJ3255] 地砖RoadBlocks ★★★   输入文件:block.in   输出文件:block.out   简单对比时间限制:1 s   内存限制:128 MB 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 q

luogu2865 [USACO06NOV]路障Roadblocks

#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <cassert> using namespace std; struct Edge{ int too, nxt, val; }edge[200005]; struct Node{ int idd, val; }d[25005];//开20000都会WA!不知道为什么 int

洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)

To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array is [1 3 -1 -3 5 3 6 7], and k = 3. 输入输出格式 输入格式: 输入一共有两行,第一行为n,k. 第二行为n个数(<INT_MAX). 输出格式: 输出共两行,第一行为每次窗口滑动的最小值

【POJ】3255 Roadblocks(次短路+spfa)

http://poj.org/problem?id=3255 同匈牙利游戏. 但是我发现了一个致命bug. 就是在匈牙利那篇,应该dis2单独if,而不是else if,因为dis2和dis1相对独立.有可能在前边两个if改了后还有更优的次短路. 所以,,wikioi那题太水,让我水过了.. #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <

【点分治】【POJ 1741】【cogs 1714】树上的点对

1714. [POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] 给一棵有n个节点的树,每条边都有一个长度(小于1001的正整数). 定义dist(u,v)=节点u到节点v的最短路距离. 给出一个整数k,我们称顶点对(u,v)是合法的当且仅当dist(u,v)不大于k. 写一个程序,对于给定的树,计算有多少对顶点对是合法的. [输入格式] 输入包含多

POJ——T2186 Popular Cows || 洛谷——P2341 [HAOI2006]受欢迎的牛

http://poj.org/problem?id=2186 || https://www.luogu.org/problem/show?pid=2341 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33470   Accepted: 13634 Description Every cow's dream is to become the most popular cow in the herd. In a herd

洛谷 P1129 BZOJ 1059 cogs 660 [ZJOI2007]矩阵游戏

题目描述 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏――矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作: 行交换操作:选择矩阵的任意两行,交换这两行(即交换对应格子的颜色) 列交换操作:选择矩阵的任意两列,交换这两列(即交换对应格子的颜色) 游戏的目标,即通过若干次操作,使得方阵的主对角线(左上角到右下角的连线)上的格子均为黑色. 对于某些关卡,小Q百思不得其解,以致他开始怀疑这些关卡是不是根本就是无解的!

COGS——C 908. 校园网 || 洛谷——P 2746 [USACO5.3]校园网Network of Schools

http://www.cogs.pro/cogs/problem/problem.php?pid=908   ||  https://www.luogu.org/problem/show?pid=2746 ★★   输入文件:schlnet.in   输出文件:schlnet.out   简单对比时间限制:1 s   内存限制:128 MB USACO/schlnet(译 by Felicia Crazy) 描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软