• 求次短路和路径条数


    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    
    const int maxn=100000+10;
    const int nil=1000000000;
    
    struct my{
           int next;
           int v;
           int w;
    };
    
    bool vis[maxn][3];
    int cnt[maxn][3],dist[maxn][3],fa,adj[maxn];
    int n,m;
    my bian[maxn*2];
    
    void myinsert(int u,int v,int w){
         bian[++fa].v=v;
         bian[fa].next=adj[u];
         bian[fa].w=w;
         adj[u]=fa;
    }
    
    
    void dijikstar(int s,int t){
         for (int i=1;i<=n;i++) dist[i][1]=dist[i][2]=nil;
         cnt[s][1]=1;
         dist[s][1]=0;
         int falg,u;
         for (int i=1;i<2*n;i++){
                int minn=nil;
            for (int j=1;j<=n;j++){
                if(!vis[j][1]&&dist[j][1]<minn){
                    minn=dist[j][1];
                    u=j;
                    falg=1;
                }
                else if(!vis[j][2]&&dist[j][2]<minn){
                    minn=dist[j][2];
                    u=j;
                    falg=2;
                }
            }
            if(minn==nil) break;
            vis[u][falg]=1;
            for (int j=adj[u];j;j=bian[j].next){
                int v=bian[j].v;
                int w=bian[j].w;
                if(dist[v][1]>minn+w){
                    dist[v][2]=dist[v][1];
                    cnt[v][2]=cnt[v][1];
                    dist[v][1]=minn+w;
                    cnt[v][1]=cnt[u][falg];
                }
                else if(dist[v][1]==minn+w) cnt[v][1]+=cnt[u][falg];
                else if(dist[v][2]>minn+w){
                    dist[v][2]=minn+w;
                    cnt[v][2]=cnt[u][falg];
                }
                else if(dist[v][2]==minn+w) cnt[v][2]+=cnt[u][falg];
            }
         }
         int ans=0;
         if(dist[t][2]==dist[t][1]+1) ans=cnt[t][2]+cnt[t][1];
         else ans=cnt[t][1];
         printf("%d
    ",ans);
    }
    
    int main(){
        int u,v,w;
        int t;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&n,&m);
            int s,t;
            memset(bian,0,sizeof(bian));
            memset(adj,0,sizeof(adj));
            memset(cnt,0,sizeof(cnt));
            memset(vis,0,sizeof(vis));
            for (int i=1;i<=m;i++){
                scanf("%d%d%d",&u,&v,&w);
                myinsert(u,v,w);
            }
            scanf("%d%d",&s,&t);
            dijikstar(s,t);
        }
    return 0;
    }
  • 相关阅读:
    hdu1242 Rescue BFS广搜 + 优先队列
    hdu 1430 魔板
    康托展开
    hdu 4394 Digital Square(bfs)
    hdu 1969 Pie
    KMP模板
    hdu 1846 Brave Game
    循环赛日程表
    hdu 1022 Train Problem I
    整数划分问题
  • 原文地址:https://www.cnblogs.com/lmjer/p/9338368.html
Copyright © 2020-2023  润新知