您的位置:首页 > 其它

POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks

2017-08-24 07:30 519 查看

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: A, B, 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、、、无向图,双向边!!啊啊啊
#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;

const int INF(0x3f3f3f3f);
const int N(5000+105);
const int M(100000+5);

int m,n,head
,sumedge;
struct Edge
{
int v,next,w;
Edge(int v=0,int next=0,int w=0):
v(v),next(next),w(w){}
}edge[M<<1];
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
;
int d1
,d2
;
inline void Dijkstar()
{
for(int i=1;i<=n;i++) d1[i]=d2[i]=INF;
priority_queue<Node>que; Node u,to;
u.dis=d1[1]=0; vis[1]=1;
u.now=1; 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=0; register char ch=getchar();
for(;ch>'9'||ch<'0';) ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
}

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
);
return 0;
}

int I_want_AC=AC();
int main(){;}
Dijkstra AC

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: