hdu1688 Sightseeing(次短路)

题目链接:

hdu1688

题意:

给你n个点,m条有向边,问你从s走到t点最短路加上最短路权值加一的路径条数

数据范围:

2n1000,1m100001s,tn,st

题解:

我们相当于求最短路和次短路,注意这里的次短路是权值的次短。然后注意处理最小和次小的关系!!

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
const int  MOD = 1000000007;
const int N = 1000 + 15;
const int maxn = 1e4+ 14;
const int letter = 130;
const int INF = 1e9;
const double pi=acos(-1.0);
const double eps=1e-8;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,s,t,head[N],tot;
int dis[N][2],vis[N][2],path[N][2];
struct edges{
    int to,next,val;
}e[maxn];
void add(int u,int v,int val){
    e[tot].val=val,e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
struct node{
    int x,val,mark;
    node(){}
    node(int x,int val,int mark):x(x),val(val),mark(mark){}
    bool operator < (const node &p) const{
        return val>p.val;
    }
};
priority_queue<node>pq;
void dijstra(int s,int t){
    while(!pq.empty()) pq.pop();
    for(int i=1;i<=n;i++) dis[i][0]=dis[i][1]=INF;
    clr(vis,0),clr(path,0);
    dis[s][0]=0;
    path[s][0]=1;
    pq.push(node(s,0,0));
    while(!pq.empty()){
        node now=pq.top();
        pq.pop();
        int x=now.x,mark=now.mark;
        if(vis[x][now.mark]) continue;
        vis[x][now.mark]=1;
        for(int i=head[x];i!=-1;i=e[i].next){

            int to=e[i].to,val=e[i].val;
            if(dis[x][mark]+val<dis[to][0]){
                if(dis[to][0]!=INF){
                    dis[to][1]=dis[to][0];
                    path[to][1]=path[to][0];
                    pq.push(node(to,dis[to][1],1));
                }
                dis[to][0]=dis[x][mark]+val;
                path[to][0]=path[x][mark];
                pq.push(node(to,dis[to][0],0));
            }
            else if(dis[x][mark]+val==dis[to][0]){
                path[to][0]+=path[x][mark];
            }
            else if(dis[x][mark]+val<dis[to][1]){
                dis[to][1]=dis[x][mark]+val;
                path[to][1]=path[x][mark];
                pq.push(node(to,dis[to][1],1));
            }
            else if(dis[x][mark]+val==dis[to][1]){
                path[to][1]+=path[x][mark];
            }
        }
    }
    ///for(int i=1;i<=n;i++) printf("i = %d %d %d\n",i,dis[i][0],dis[i][1]);
    int ans=path[t][0];
    if(dis[t][0]==dis[t][1]-1) ans+=path[t][1];
    printf("%d\n",ans);
}
int main(){
    int T,u,v,w;
    scanf("%d",&T);
    while(T--){
        clr(head,-1),tot=0;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        scanf("%d%d",&s,&t);
        dijstra(s,t);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值