题目描述
贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友。贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路。 贝茜所在的乡村有R(1<=R<=100,000)条双向道路,每条路都联结了所有的N(1<=N<=5000)个农场中的某两个。贝茜居住在农场1,她的朋友们居住在农场N(即贝茜每次旅行的目的地)。 贝茜选择的第二短的路径中,可以包含任何一条在最短路中出现的道路,并且,一条路可以重复走多次。当然咯,第二短路的长度必须严格大于最短路(可能有多条)的长度,但它的长度必须不大于所有除最短路外的路径的长度。
输入输出格式
输入格式:
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)
输出格式:
Line 1: The length of the second shortest path between node 1 and node N
代码
这道题是次短路的题,我们可以用spfa来求解
需要开两个数组dist1[]记录最短路,dist2[]记录次短路
dist1[v]>dist1[u]+w(u, v) 显然要更新dist1,而因为dist1是最短路,所以显然要先更新dist2等于原来的dist1再更新dist1
dist2[v]>dis1t[u]+w(u, v) && dist1[v]>dist1[u]+w(u, v) 因为现在 d1[u]+w(u, v) 不是最短路,但是又比次短路小,那么显然要更新次短路
dist2[v]>dist2[u]+w(u, v) 这时次短路比次短路短,那么肯定要更新次短路.
A C历程
一开始只想到上面的第一种和第二种情况,没有考虑第三种情况,所以只有50分,所有没AC的点的数据都要大些。
然后我参考了hzwer大神的题解知道了第三种情况,但我将次短路也初始化为零了,所以每个点的次短路都变成了最短了,于是我就在判断第三种情况下加了判断dist1[v]
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<deque>
#include<cstring>
const int maxn = 5005;
using namespace std;
struct edge
{
int v;
int next;
int w;
}e[200050];
int ans,dist[maxn],p[maxn],dist2[maxn],head[maxn],cnt;
bool vis[maxn];
void add(int u,int v,int w)
{
e[++cnt] = (edge){v,head[u],w};
head[u] = cnt;
}
int read() //读入优化
{
int ans=0;char ch = getchar();
while(ch<'0'||ch>'9') ch = getchar();
while(ch>='0'&&ch<='9') {ans = ans *10 + ch - '0';ch = getchar();}
return ans;
}
void spfa()
{
int v,u;
deque<int> q;
memset(dist,0x7f,sizeof(dist));
memset(vis,false,sizeof(vis));
q.push_back(1);
dist[1] = 0;
vis[1] = true;
while(!q.empty())
{
u = q.front();
q.pop_front();
for(int i=head[u];i;i=e[i].next)
{
v = e[i].v;
if(dist[v]>dist[u]+e[i].w)
{
dist2[v] = dist[v];
dist[v] = dist[u] + e[i].w;
if(!vis[v])
{
q.push_back(v);
vis[v] = true;
}
}
else if(dist2[v]>dist[u]+e[i].w)
{
dist2[v] = dist[u] + e[i].w;
}
}
vis[u] = false;
}
}
int main()
{
int n,m;
n=read();
m=read();
for(int i=1;i<=m;i++)
{
int u,v,w;
u=read();
v=read();
w=read();
add(u,v,w);
add(v,u,w);
}
spfa();
ans = dist2[n];
printf("%d",ans);
return 0;
}
90分
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<deque>
#include<cstring>
const int maxn = 5005;
using namespace std;
struct edge
{
int v;
int next;
int w;
}e[200050];
int ans,dist[maxn],p[maxn],dist2[maxn],head[maxn],cnt;
bool vis[maxn];
void add(int u,int v,int w)
{
e[++cnt] = (edge){v,head[u],w};
head[u] = cnt;
}
int read() //读入优化
{
int ans=0;char ch = getchar();
while(ch<'0'||ch>'9') ch = getchar();
while(ch>='0'&&ch<='9') {ans = ans *10 + ch - '0';ch = getchar();}
return ans;
}
void spfa()
{
int v,u;
deque<int> q;
memset(dist,0x7f,sizeof(dist));
memset(dist2,0x7f,sizeof(dist2));
memset(vis,false,sizeof(vis));
q.push_back(1);
dist[1] = 0;
dist2[1] = 0;
vis[1] = true;
while(!q.empty())
{
u = q.front();
q.pop_front();
for(int i=head[u];i;i=e[i].next)
{
v = e[i].v;
if(dist[v]>dist[u]+e[i].w)
{
dist2[v] = dist[v];
dist[v] = dist[u] + e[i].w;
if(!vis[v])
{
q.push_back(v);
vis[v] = true;
}
}
else if(dist2[v]>dist[u]+e[i].w&&dist[v]!=dist[u]+e[i].w)
{
dist2[v] = dist[u] + e[i].w;
if(!vis[v])
{
q.push_back(v);
vis[v] = true;
}
}
if(dist2[u]+e[i].w<dist2[v]&&dist[v]<dist2[u]+e[i].w)
{
dist2[v] = dist2[u] + e[i].w;
if(!vis[v])
{
q.push_back(v);
vis[v] = true;
}
}
}
vis[u] = false;
}
}
int main()
{
int n,m;
n=read();
m=read();
for(int i=1;i<=m;i++)
{
int u,v,w;
u=read();
v=read();
w=read();
add(u,v,w);
add(v,u,w);
}
spfa();
ans = dist2[n];
printf("%d",ans);
return 0;
}
100分
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<deque>
#include<cstring>
const int maxn = 5005;
using namespace std;
struct edge
{
int v;
int next;
int w;
}e[200050];
int ans,dist[maxn],p[maxn],dist2[maxn],head[maxn],cnt;
bool vis[maxn];
void add(int u,int v,int w)
{
e[++cnt] = (edge){v,head[u],w};
head[u] = cnt;
}
int read() //读入优化
{
int ans=0;char ch = getchar();
while(ch<'0'||ch>'9') ch = getchar();
while(ch>='0'&&ch<='9') {ans = ans *10 + ch - '0';ch = getchar();}
return ans;
}
void spfa()
{
int v,u;
deque<int> q;
memset(dist,0x7f,sizeof(dist));
memset(dist2,0x7f,sizeof(dist2));
memset(vis,false,sizeof(vis));
q.push_back(1);
dist[1] = 0;
vis[1] = true;
while(!q.empty())
{
u = q.front();
q.pop_front();
for(int i=head[u];i;i=e[i].next)
{
v = e[i].v;
if(dist[v]>dist[u]+e[i].w)
{
dist2[v] = dist[v];
dist[v] = dist[u] + e[i].w;
if(!vis[v])
{
q.push_back(v);
vis[v] = true;
}
}
if(dist2[v] > dist[u]+e[i].w && dist[v] < dist[u]+e[i].w)
{
dist2[v] = dist[u] + e[i].w;
if(!vis[v])
{
q.push_back(v);
vis[v] = true;
}
}
if(dist2[u]+e[i].w<dist2[v])
{
dist2[v] = dist2[u] + e[i].w;
if(!vis[v])
{
q.push_back(v);
vis[v] = true;
}
}
}
vis[u] = false;
}
}
int main()
{
int n,m;
n=read();
m=read();
for(int i=1;i<=m;i++)
{
int u,v,w;
u=read();
v=read();
w=read();
add(u,v,w);
add(v,u,w);
}
spfa();
ans = dist2[n];
printf("%d",ans);
return 0;
}