POJ3255(次短路)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12480 | Accepted: 4416 |
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 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
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
次短路两种解法:spfa迭代、spfa+A*
spfa迭代:
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int MAXN=5005; const int INF=0x3f3f3f3f; struct Edge{ int to,w,net; }es[200005]; int n,m; int head[MAXN],tot; void addedge(int u,int v,int w) { es[tot].to=v; es[tot].w=w; es[tot].net=head[u]; head[u]=tot++; } int d[MAXN],vis[MAXN],second[MAXN]; void spfa(int src) { for(int i=1;i<=n;i++) { second[i]=INF; d[i]=INF; vis[i]=0; } d[src]=0; vis[src]=1; queue<int> que; que.push(src); while(!que.empty()) { int u=que.front();que.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=es[i].net) { Edge e=es[i]; bool tag=false; if(d[e.to]>d[u]+e.w) { second[e.to]=d[e.to]; d[e.to]=d[u]+e.w; tag=true; } else if(d[e.to]<d[u]+e.w&&d[u]+e.w<second[e.to]) { second[e.to]=d[u]+e.w; tag=true; } if(second[e.to]>second[u]+e.w) { second[e.to]=second[u]+e.w; tag=true; } if(tag) { if(!vis[e.to]) { vis[e.to]=1; que.push(e.to); } } } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(head,-1,sizeof(head)); tot=0; for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } spfa(1); printf("%d\n",second[n]); } return 0; }
spfa+A*算法
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int MAXN=5005; const int INF=0x3f3f3f3f; struct Edge{ int to,w,net; }es[200005]; int n,m; int head[MAXN],tot; void addedge(int u,int v,int w) { es[tot].to=v; es[tot].w=w; es[tot].net=head[u]; head[u]=tot++; } int d[MAXN],vis[MAXN]; void spfa(int src) { for(int i=1;i<=n;i++) { d[i]=INF; vis[i]=0; } queue<int> que; d[src]=0; que.push(src); while(!que.empty()) { int u=que.front();que.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=es[i].net) { if(d[es[i].to]>d[u]+es[i].w) { d[es[i].to]=d[u]+es[i].w; if(!vis[es[i].to]) { vis[es[i].to]=1; que.push(es[i].to); } } } } } struct Node{ int u,g,f; Node(){} Node(int u,int g,int f) { this->u=u; this->g=g; this->f=f; } bool operator<(const Node e) const { return e.f < f; } }; void Astar(int src,int ter) { priority_queue<Node> que; que.push(Node(src,0,d[src])); int k=0; while(!que.empty()) { Node now=que.top();que.pop(); if(now.u==ter) { k++; if(k==2) { printf("%d\n",now.f); return ; } } for(int i=head[now.u];i!=-1;i=es[i].net) { que.push(Node(es[i].to,now.g+es[i].w,now.g+es[i].w+d[es[i].to])); } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(head,-1,sizeof(head)); tot=0; for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } spfa(n); Astar(1,n); } return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步