hdu 3191 How Many Paths Are There(次短路+条数)

How Many Paths Are There

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1493    Accepted Submission(s): 538


Problem Description
  oooccc1 is a Software Engineer who has to ride to the work place every Monday through Friday. For a long period, he went to office with the shortest path because he loves to sleep late…Time goes by, he find that he should have some changes as you could see, always riding with the same path is boring.
  One day, oooccc1 got an idea! Why could I take another path? Tired at all the tasks he got, he got no time to carry it out. As a best friend of his, you’re going to help him!
  Since oooccc1 is now getting up earlier, he is glad to take those paths, which are a little longer than the shortest one. To be precisely, you are going to find all the second shortest paths.
  You would be given a directed graph G, together with the start point S which stands for oooccc’1 his house and target point E presents his office. And there is no cycle in the graph. Your task is to tell him how long are these paths and how many there are.
 

Input
There are some cases. Proceed till the end of file.
The first line of each case is three integers N, M, S, E (3 <= N <= 50, 0 <= S , E <N)
N stands for the nodes in that graph, M stands for the number of edges, S stands for the start point, and E stands for the end point.
Then M lines follows to describe the edges: x y w. x stands for the start point, and y stands for another point, w stands for the length between x and y. 
All the nodes are marked from 0 to N-1.
 

Output
For each case,please output the length and count for those second shortest paths in one line. Separate them with a single space.
 

Sample Input
  
  
3 3 0 2 0 2 5 0 1 4 1 2 2
 

Sample Output
  
  
6 1
题意:求次短路和次短路条数

思路: 迪杰斯特拉变形可求次短路,具体实现可见点击打开链接

我这里不太习惯优先队列,所以写了个朴素算法。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 60
#define INF 100000000
int n,m;
int vis[N][2];
int d[N][2],dp[N][2];
int ma[N][N];
void init()
{
    for(int i=0; i<=n; i++)
        for(int j=0; j<=n; j++)
            ma[i][j]=INF;
}
void djstl(int s,int e)
{
    for(int i=0; i<n; i++)
        d[i][0]=d[i][1]=INF;
    memset(vis,0,sizeof(vis));
    memset(dp,0,sizeof(dp));
    d[s][0]=0;
    dp[s][0]=1;
    while(1)
    {
        int maxn=INF;
        int v,flag;
        for(int j=0; j<n; j++)
        {
            if(!vis[j][0]&&maxn>d[j][0])
            {
                maxn=d[j][0];
                v=j;flag=0;
            }
            else if(!vis[j][1]&&maxn>d[j][1])
            {
                maxn=d[j][1];
                v=j;flag=1;
            }
        }
        if(v==e&&flag==1) break;
        if(maxn==INF) break;
        vis[v][flag]=1;
        for(int j=0; j<n; j++)
        {
            if(!vis[j][0]&&d[v][flag]+ma[v][j]<d[j][0])
            {
                d[j][1]=d[j][0];
                d[j][0]=d[v][flag]+ma[v][j];
                dp[j][1]=dp[j][0];
                dp[j][0]=dp[v][flag];
            }
            else if(!vis[j][0]&&d[v][flag]+ma[v][j]==d[j][0])
                dp[j][0]+=dp[v][flag];
            else if(!vis[j][1]&&d[v][flag]+ma[v][j]<d[j][1])
            {
                d[j][1]=d[v][flag]+ma[v][j];
                dp[j][1]=dp[v][flag];
            }
            else if(!vis[j][1]&&d[v][flag]+ma[v][j]==d[j][1])
                dp[j][1]+=dp[v][flag];
        }
    }
}

int main()
{
    int s,e;
    int x,y,w;
    while(~scanf("%d %d %d %d",&n,&m,&s,&e))
    {
        init();
        for(int i=0; i<m; i++)
        {
            scanf("%d %d %d",&x,&y,&w);
            ma[x][y]=w;
        }
        djstl(s,e);
        printf("%d %d\n",d[e][1],dp[e][1]);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值