1005 最短路

                                                                                                         A Walk Through the Forest
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 9
Problem Description
Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.

Input
Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses. There is at most one path between any pair of intersections.

Output
For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647

Sample Input
  
  
5 6 1 3 2 1 4 2 3 4 3 1 5 12 4 2 34 5 2 24 7 8 1 3 1 1 4 1 3 7 1 7 4 1 7 5 1 6 7 1 5 2 1 6 2 1 0

Sample Output
  
  
2 4


题意:从一个点到另一个点的有多少条最短路

思路:先用dij求出一个最短路 作为比较的值 然后再用dfs进行记忆化搜索找到所有的最短路

 

代码:

#include<iostream>
#include<cstdio>
#include<string.h>
#define N 2000
#define INF 2000000000
using namespace std;
int map[N][N],dir[N],vis[N],d[N],p[N];
void dijkstra(int s,int n)
{
    
     int i,j,k,min;
     for(i=1;i<=n;i++)
     {
         dir[i]=map[s][i];
     }
      memset(vis,false,sizeof(vis));
     d[s]=0;
     vis[s]=true;
     for(i=1;i<n;i++)//注意不是小于等于 
     {
         min=INF;
         for(j=1;j<=n;j++)
         {
             if(!vis[j]&&min>dir[j])
             {
                 min=dir[j];
                 k=j;
             }
         }
         d[k]=min;
         vis[k]=true;
         for(j=1;j<=n;j++)
         {
             if(!vis[j]&&dir[j]>map[k][j]+d[k])
                 dir[j]=map[k][j]+d[k];
         }
     }
}
int DFS(int s,int n)
{
     if(p[s]) return p[s];
     if(s==2)  return 1;
     int i,sum=0;
     for(i=1;i<=n;i++)
     {
         if(map[s][i]<INF&&d[s]>d[i])
         {
             if(p[i]) 
             sum=sum+p[i];
             else 
             sum=sum+DFS(i,n);
         }
     }
     sum=sum+p[s];
     p[s]=sum;
     return p[s];
}
 

/*
int DFS(int now,int n)  //这个dfs记忆化搜索一直没看懂  
{  
    if(now==2)  return 1;  
    
    if(p[now])  return p[now]; 
     
    for(int i=0;i<=n;i++)  
    {  
        if(dir[now]>dir[map[now][i]]) 
          
        p[now]+=DFS(map[now][i],n);  
    }  
    return p[now];  
}  
 */
int main()
{
     int i,j,n,m,u,v,w;
     while(cin>>n&&n)
     {
         cin>>m;
         memset(p,0,sizeof(p));
         for(i=1;i<=n;i++)
         {
             for(j=1;j<=n;j++)
             {
                 map[i][j]=INF;
             }
         }
         for(i=0;i<m;i++)
         {
             cin>>u>>v>>w;
             map[u][v]=map[v][u]=w;
         }
         dijkstra(2,n);
         cout<<DFS(1,n)<<endl;
     }
     return 0;
}


 

以下是C++实现的Yen算法求k短路的模板代码: ```c++ #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm> using namespace std; const int MAXN = 1005; const int MAXM = 200005; const int INF = 0x3f3f3f3f; int n, m, k, dis[MAXN], vis[MAXN], cnt; int head[MAXN], nxt[MAXM], ver[MAXM], edge[MAXM]; int ans[MAXN]; struct Node { int dis, id; bool operator < (const Node& nd) const { return dis > nd.dis; } }; struct Edge { int u, v, w; } e[MAXM]; priority_queue <Node> q; vector <int> vec[MAXN]; void add(int u, int v, int w) { ver[++cnt] = v; edge[cnt] = w; nxt[cnt] = head[u]; head[u] = cnt; } void dijkstra(int s) { memset(vis, 0, sizeof(vis)); memset(dis, INF, sizeof(dis)); dis[s] = 0; q.push((Node){0, s}); while (!q.empty()) { Node t = q.top(); q.pop(); int u = t.id; if (vis[u]) continue; vis[u] = 1; for (int i = head[u]; i; i = nxt[i]) { int v = ver[i], w = edge[i]; if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; if (!vis[v]) q.push((Node){dis[v], v}); } } } } void init() { cnt = 0; memset(head, 0, sizeof(head)); } void Yen(int s, int t) { dijkstra(s); if (dis[t] == INF) return; priority_queue <Node> pq; pq.push((Node){dis[t], t}); for (int i = 1; i <= k; ++i) { if (pq.empty()) break; int u = pq.top().id; pq.pop(); ans[i] = dis[u]; for (int j = 1; j <= n; ++j) vec[j].clear(); for (int j = head[u]; j; j = nxt[j]) vec[ver[j]].push_back(j); for (int p = 1; p <= i; ++p) { int d = ans[p], v = e[p].u, w = e[p].w; for (int j = 0; j < vec[v].size(); ++j) { int k = vec[v][j]; if (k == w) continue; if (p == 1 && j == 0) continue; int tmp = dis[ver[k]]; dis[ver[k]] = d + edge[k] - dis[v] + dis[ver[k]]; e[p].u = ver[k], e[p].w = k; pq.push((Node){dis[ver[k]] + ans[p - 1], ver[k]}); if (p == k) ans[p] = dis[t]; } e[p].u = v, e[p].w = w; memcpy(head, nxt, sizeof(nxt)); } } for (int i = 1; i <= k; ++i) printf("%d ", ans[i]); } int main() { scanf("%d%d%d", &n, &m, &k); init(); for (int i = 1; i <= m; ++i) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add(u, v, w); } Yen(1, n); return 0; } ``` 其中,`n`表示节点数量,`m`表示边的数量,`k`表示要求的第k短路。 在代码中,我们首先使用Dijkstra算法求出源点到终点的短路`dis`,然后使用优先队列`pq`存储当前的短路,对于每一次迭代,我们从`pq`中取出距离终点近的节点`u`,然后枚举所有与`u`相邻的边,计算新的路径长度,并将其插入`pq`中。后,我们将前`k`个短路的长度存储在`ans`数组中输出即可。 需要注意的是,在每一次迭代之后,我们需要将邻接表`head`恢复成初始状态,否则会影响后续的计算。同时,在计算新的路径长度时,需要注意避免重复计算,具体实现见代码注释。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值