hdu 1688

最短路与次短路条数

#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);
        e[cout].u = y;
        e[cout].val = cost;
        e[cout].next = p[x];
        p[x] = cout++;
    }
}
int dijkstra(int s,int t,int n,int m)
{
    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 num = cnt[t][0];
    if(d[t][0] + 1 == d[t][1]) num+=cnt[t][1];
    return num;
}
int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        add(n,m);
        int s,t;
        scanf("%d %d",&s,&t);
        int ans = dijkstra(s,t,n,m);
        printf("%d\n",ans);
    }
    return 0;
}

hdu 1688

时间: 2024-10-14 22:31:21

hdu 1688的相关文章

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 1688 Sightseeing

题目链接:Sightseeing 题意:求最短路和比最短路长度+1的所有路径条数. 附代码:用数组记录最短和次短路径的长度和条数,一次更新,直到没有边可以更新. #include <stdio.h> #include <string.h> #include <iostream> #include <vector> using namespace std; #define maxn 1010 struct Node { int to, val; Node(in

hdu 1688 Sightseeing【最短路,次短路条数】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题意:求最短路和次短路条数如果次短路长度=最短路长度+1 这输出次短路条数+最短路条数,否则输出最短路条数 分析:这是到模版题,献上模版: #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<queue> #include<

HDU 1688 Sightseeing 【输出最短路+次短路条数】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思路: 1.最短路和次短路是紧密相连的,在最短路松弛操作中,当我们找到一条更短的路径,也就意味着之前的路径不再是最短路,而成为了次短路,利用这个关系可以实现状态的转移. 2.好久没写优先队列了,都忘记了加个 priority_queue, 这样才能写重载,才能排序. 注释在代码里: 1 #includ

hdu 1688 Sightseeing (最短路径)

Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 639    Accepted Submission(s): 249 Problem Description Tour operator Your Personal Holiday organises guided bus trips across the Benel

图论 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 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

hdu图论题目分类

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

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