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): 1103    Accepted Submission(s): 372


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
 

Author
ZSTU
 

Source
解题思路:次短路的模板题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define Inf 999999999
using namespace std;
int head[55],k,dis[2][55],dp[2][55];
struct Edge
{
    int e;
    int w;
    int mark;
    int next;
    bool operator <(const Edge &a)const
    {
        if(w!=a.w)
            return w>a.w;
        else
            return e>a.e;
    }
}edge[3000],t1,t2;
void add(int s,int e,int w)
{
    edge[k].e=e;
    edge[k].w=w;
    edge[k].next=head[s];
    head[s]=k++;
}
//dp[i][0]表示到达点i最短的路有多少条,dp[i][1]表示次短的条数  
//dist[i][0]表示到达点i最短路的长度,dist[i][1]表示次短路的长度
/*
用v去松驰u时有四种情况 (设当前dist[v][cas]) 
情况1:dist[v][cas]+w(v,u)<dist[u][1],找到一个更短的距离,则把原来最短的距离作为次短的距离,同时更新最短的.即
dist[u][1]=dist[u][0]   
dist[u][0]=dist[v][cas]+w(v,u);   
dp[u][1]=dp[u][0]   
dp[u][0]=dp[v][cas], 
把Node(dist[u][0],u,1)和Node(dist[u][1],u,2)放入队列 
情况2:dist[v][cas]+w(v,u)==dist[u][0],找到一条新的相同距离的最短路,则dp[u][1]+=dp[v][cas],其他不用更新,也不入队 
情况3:dist[v][cas]+w(v,u)<dist[u][1],不可以更新最短距离,但可以更新次短的,则更新dist[u][2]和dp[u][2]  
dist[u][1]=dist[v][cas]+w(v,u);  
dp[u][1]=dp[v][cas]; 
把Node(dist[u][1],u,2)放入队列 
情况4:dist[v][cas]+w(v,u)==dist[u][1] 找到一条新的相同距离的次短路,则dp[u][2]+=dp[v][cas],其他不更新。 
*/
void dij(int s,int n)
{
    priority_queue<Edge> x;
    int i,st,ed;
    t1.e=s;t1.mark=0;t1.w=0;
    x.push(t1);
    bool visit[2][55];
    memset(visit,false,sizeof(visit));
    memset(dp,0,sizeof(dp));
    dp[0][s]=1;
    for(i=0;i<n;i++)
        dis[0][i]=dis[1][i]=Inf;
    dis[0][s]=0;
    while(!x.empty())
    {
        t1=x.top();
        x.pop();
        st=t1.e;
        if(visit[t1.mark][st])
            continue;
        visit[t1.mark][st]=true;
        for(i=head[st];i!=-1;i=edge[i].next)
        {
            ed=edge[i].e;
            if(!visit[0][ed]&&dis[0][ed]>t1.w+edge[i].w)
            {
                if(dis[0][ed]!=Inf)
                {
                    t2.e=ed;t2.w=dis[0][ed];t2.mark=1;
                    x.push(t2);
                    dis[1][ed]=dis[0][ed];
                    dp[1][ed]=dp[0][ed];
                }
                dp[0][ed]=dp[t1.mark][st];
                dis[0][ed]=t1.w+edge[i].w;
                t2.e=ed;t2.w=dis[0][ed];t2.mark=0;
                x.push(t2);
            }
            else if(!visit[0][ed]&&dis[0][ed]==t1.w+edge[i].w)
                dp[0][ed]+=dp[t1.mark][st];
            else if(!visit[1][ed]&&dis[1][ed]>t1.w+edge[i].w)
            {
                dis[1][ed]=t1.w+edge[i].w;
                dp[1][ed]=dp[t1.mark][st];
                t2.e=ed;t2.w=dis[1][ed];t2.mark=1;
                x.push(t2);
            }
            else if(!visit[1][ed]&&dis[1][ed]==t1.w+edge[i].w)
                dp[1][ed]+=dp[t1.mark][st];
        }
    }
}
int main()
{
    int m,n,s,e,a,b,c;
    while(~scanf("%d%d%d%d",&n,&m,&s,&e))
    {
        k=0;
        memset(head,-1,sizeof(head));
        while(m--)
        {
           scanf("%d%d%d",&a,&b,&c);
           add(a,b,c);
        }
        dij(s,n);
        printf("%d %d\n",dis[1][e],dp[1][e]);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值