zoukankan      html  css  js  c++  java
  • SDUT 2622 最短路径(Dijkstra)

    点我看题目

    题意 :中文不详述。

    思路 :因为这个题加了一个要求就是路径数目得是x的倍数。所以在原来算法的一维dis数组增加到二维,用来存走的路径数%x。也可以用spfa做。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <iostream>
    
    using namespace std ;
    
    #define LL long long
    const int maxn = 110 ;
    const int maxm = 10010 ;
    bool vis[maxn][maxn] ;
    const LL INF = 1LL<<60 ;
    int cnt = 0 ;
    int head[maxn] ;
    LL dist[maxn][maxn] ;
    int n , m ;
    
    struct node
    {
        int u,v,w ;
        int next ;
    }Edge[maxm] ;
    
    void addedge(int u,int v,int w)
    {
        Edge[cnt].u = u ;
        Edge[cnt].v = v ;
        Edge[cnt].w = w ;
        Edge[cnt].next = head[u]  ;
        head[u] = cnt++ ;
    }
    
    LL dijkstra(int s,int t,int x)
    {
        for(int i = 0 ; i < n ; i++)
        for(int j = 0 ; j < x ; j++)
        {
            dist[i][j] = INF ;
            vis[i][j] = false ;
        }
        dist[s][0] = 0 ;
        while(true)
        {
            LL minn = INF ;
            int u = -1 ;
            int flag , xx ;
            for(int i = 0 ; i < n ; i++)
            {
                for(int j = 0 ; j < x ; j++)
                {
                    if(!vis[i][j] && minn > dist[i][j])
                    {
                        minn = dist[i][j] ;
                        u = i ;
                        flag = j ;
                        xx = (flag+1)%x ;
                    }
                }
            }
            if(u == -1) return -1 ;
            vis[u][flag] = true ;
            if(vis[t][0]) return dist[t][0] ;
            for(int j = head[u] ; j  != -1 ; j = Edge[j].next)
            {
                int v = Edge[j].v ;
                if(!vis[v][xx] && minn + Edge[j].w < dist[v][xx])
                dist[v][xx] = minn + Edge[j].w ;
            }
        }
    }
    int main()
    {
        int T,s,t,x ;
        scanf("%d",&T) ;
        while(T--)
        {
            cnt = 0 ;
            memset(head,-1,sizeof(head)) ;
        //    memset(vis,false,sizeof(vis) ) ;
            scanf("%d %d",&n,&m) ;
            int u,v, w ;
            for(int i = 0 ; i < m ; i++)
            {
                scanf("%d %d %d",&u,&v,&w) ;
                addedge(u,v,w) ;
            }
            scanf("%d %d %d",&s,&t,&x) ;
            LL ans = dijkstra(s,t,x) ;
            if(ans != -1)
                printf("%lld
    ",dist[t][0]) ;
            else printf("No Answer!
    ") ;
        }
        return 0 ;
    }
    View Code
  • 相关阅读:
    CentOS 7 Docker基本特性
    linux go环境安装
    Docker部署Golang
    高可用Redis服务架构分析与搭建
    linux常用基本命令
    docker容器
    Linux 配置网络连接
    自定义泛型委托对象
    sql游标使用
    CSS 实用实例
  • 原文地址:https://www.cnblogs.com/luyingfeng/p/3621844.html
Copyright © 2011-2022 走看看