hdu 3191

次短路与条数

#include <stdio.h>
#include <string.h>
#define N 10005
#define INF 0x3f3f3f3f
struct Edge{
    int u,val,next;
}e[2*N];
int p[N],vis[N][2],d[N][2],cnt[N][2];
void add(int n,int m)
{
    int i,x,y,cost,cout = 1;
    for(i = 1 ; i <= n ; i++) p[i] = -1;
    while(m--)
    {
        scanf("%d %d %d",&x,&y,&cost);
        x++;y++;
        e[cout].u = y;
        e[cout].val = cost;
        e[cout].next = p[x];
        p[x] = cout++;
    }
}
void dijkstra(int s,int t,int n)
{
    int i,x,y,temp,ok;
    for(i = 1 ; i <= n ; i++)
    {
        d[i][0] = INF;
        d[i][1] = INF;
        vis[i][0] = 0;
        vis[i][1] = 0;
    }
    d[s][0] = 0;
    cnt[s][0] = 1;
    for(i = 1 ; i <= 2*n ; i++)
    {
        temp = INF,ok ,x = -1;
        for(y = 1 ; y <= n ;y++)
            if(!vis[y][0]&&temp>d[y][0]) {
                temp = d[y][0];
                ok = 0;
                x = y;
            }
        else if(!vis[y][1]&&temp>d[y][1]) {
            temp = d[y][1];
            ok = 1;
            x = y;
        }
        if(x == -1) break;
        vis[x][ok] = 1;
        for(y = p[x] ; y!=-1; y = e[y].next)
        {
            int newd = d[x][ok]+e[y].val;
            int u = e[y].u;
            if(newd<d[u][0])
            {
                d[u][1] = d[u][0];
                d[u][0] = newd;
                cnt[u][1] = cnt[u][0];
                cnt[u][0] = cnt[x][ok];
            }else if(newd == d[u][0]){
                cnt[u][0] += cnt[x][ok];
            }else if(newd<d[u][1]){
                d[u][1] = newd;
                cnt[u][1] = cnt[x][ok];
            }else if(newd == d[u][1]){
                cnt[u][1]+=cnt[x][ok];
            }
        }
    }
}
int main()
{
    int n,m,s,t;;
    while(~scanf("%d %d",&n,&m))
    {
        scanf("%d %d",&s,&t);
        s++;t++;
        add(n,m);
        dijkstra(s,t,n);
        printf("%d %d\n",d[t][1],cnt[t][1]);
    }
    return 0;
}

hdu 3191

时间: 2024-08-30 10:07:12

hdu 3191的相关文章

POJ 3255 &amp;&amp; HDU 1688 &amp;&amp; HDU 3191 次短路问题

POJ 3255 Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7627   Accepted: 2798 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 h

HDU 3191 How Many Paths Are There

How Many Paths Are There Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 319164-bit integer IO format: %I64d      Java class name: Main oooccc1 is a Software Engineer who has to ride to the work place every M

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): 1010    Accepted Submission(s): 332 Problem Description oooccc1 is a Software Engineer who has to ride to the work place

HDU 3191 次短路长度和条数

http://www.cnblogs.com/wally/archive/2013/04/16/3024490.html http://blog.csdn.net/me4546/article/details/6584448 维护最短路长度d[i][0]和次短路d[i][1],最短路条数dp[i][0]和次短路dp[i][1] #include <iostream> #include <string> #include <cstring> #include <cs

HDU 3191 How Many Paths Are There(SPFA)

How Many Paths Are There Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1339    Accepted Submission(s): 468 Problem Description oooccc1 is a Software Engineer who has to ride to the work place

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

hdu 1979 DFS + 字典树剪枝

http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 373    Accepted Submission(s): 155 Problem Description There is a matrix of 4*4, yo

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123

HDU 4585 Shaolin(Treap找前驱和后继)

Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 3191    Accepted Submission(s): 1350 Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shao