poj3255
题目:
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 intersection N.
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)
思路:就是一道单纯的求次短路的题目,先来看如何求解次短路:如果我们要求解起点s到终点t的次短路,那么有两种可能的情况:
(1)起点s到某个顶点u的最短路+(u到t的距离)
(2)起点到某个顶点u的次短路+(u到t的距离)
因此,对于每个结点,我们记录的不仅仅是最短距离,还有次短距离,然后用类似于Dijkstra算法不断更新这两个距离即可求出次短路。
代码:
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <ctime>
#include <string>
#include <limits>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <istream>
#include <iostream>
#include <algorithm>
#define ci cin
#define co cout
#define el endl
#define Scc(c) scanf("%c",&c)
#define Scs(s) scanf("%s",s)
#define Sci(x) scanf("%d",&x)
#define Sci2(x, y) scanf("%d%d",&x,&y)
#define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%I64d",&x)
#define Scl2(x, y) scanf("%I64d%I64d",&x,&y)
#define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define Pri(x) printf("%d\n",x)
#define Prl(x) printf("%I64d\n",x)
#define Prc(c) printf("%c\n",c)
#define Prs(s) printf("%s\n",s)
#define For(i,x,y) for(int i=x;i<y;i++)
#define For_(i,x,y) for(int i=x;i<=y;i++)
#define FFor(i,x,y) for(int i=x;i>y;i--)
#define FFor_(i,x,y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define LL long long
#define ULL unsigned long long
#define MAXSIZE 100005
#define INF 0x3f3f3f3f
const int mod=1e9+7;
const double PI = acos(-1.0);
using namespace std;
typedef pair<int,int>pii;
struct edge
{
int to,w;
edge(int x,int y)
{
to=x;
w=y;
}
};
vector<edge>G[MAXSIZE];//邻接表储存
int dis[MAXSIZE];//最短
int dis2[MAXSIZE];//次短
int n,r;
void solve()
{
priority_queue<pii,vector<pii>,greater<pii> >q;//注意这个队列first存的是到每点的最短距离,second存的这个点
Mem(dis,INF);
Mem(dis2,INF);
q.push(pii(0,1));
dis[1]=0;
while(!q.empty())
{
pii p=q.top();
q.pop();
int v=p.second,d=p.first;
if(dis2[v]<d)
continue;//到v的距离比次短路短(肯定也比最短路短),终止本次循环
for(int i=0; i<G[v].size(); i++)
{
edge &e=G[v][i];
int d2=e.w+d;
if(dis[e.to]>dis[v]+e.w)//更新最短路径if(dis[e.to]>d2)
{
swap(dis[e.to],d2);
q.push(pii(dis[e.to],e.to));
}
if(dis2[e.to]>d2&&dis[e.to]<d2)//注意这个两个条件,后面那个条件可以不要
{
swap(dis2[e.to],d2);
q.push(pii(dis2[e.to],e.to));
}
}
}
Pri(dis2[n]);
}
int main()
{
Sci2(n,r);
For_(i,1,r)
{
int u,v,w;
Sci3(u,v,w);
G[u].push_back(edge(v,w));
G[v].push_back(edge(u,w));
}
solve();
return 0;
}
^__^好吧,我现在已经废到连一个次短路径都看不懂了,唉,这道题写于大二上学期的期末考试考完的这天晚上,浪了一个学期,感觉啥都没干,此时想到暑假结束时的目标。。。。。。。。君子立长志,小人常立志 真香!寒假快开始了,真的得改改了!