zoukankan      html  css  js  c++  java
  • HDU 4725 The Shortest Path in Nya Graph

    This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.
    The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total.
    You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost.
    Besides, there are M extra edges, each connecting a pair of node u and v, with cost w.
    Help us calculate the shortest path from node 1 to node N.

    Input

    The first line has a number T (T <= 20) , indicating the number of test cases.
    For each test case, first line has three numbers N, M (0 <= N, M <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
    The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to.
    Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), which means there is an extra edge, connecting a pair of node u and v, with cost w.

    Output

    For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
    If there are no solutions, output -1.

    Sample Input

    2
    3 3 3
    1 3 2
    1 2 1
    2 3 1
    1 3 3
    
    3 3 3
    1 3 2
    1 2 2
    2 3 2
    1 3 4

    Sample Output

    Case #1: 2
    Case #2: 3

     建图跑最短路,

    把图层看作一个点N+1到2*N,图层与图层建边,点与相邻图层建边,图层与本身点建边;点与点建边

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    #include<map>
    
    #define inf 0x3f3f3f3f
    
    #define mem(a,b) memset(a,b,sizeof(a));
    
    const int maxn=1e5+10;
    
    using namespace std;
    
    struct edge{
        int u,v,w,next;
    }e[10*maxn];
    
    int g[10*maxn],n,m,cost,d[2*maxn],tot,lay[2*maxn],arr[2*maxn];
    
    bool vis[2*maxn];
    int spfa(int st){
        queue<int>q;
        mem(vis,false);
        mem(d,inf);
        //cout<<d[1]<<endl;
        vis[st]=true;
        d[st]=0;
        q.push(st);
        while(!q.empty())
        {
            int now=q.front();
            q.pop();
            vis[now]=false;
            for(int i=g[now];i>0;i=e[i].next)
            {
                int u=e[i].u,v=e[i].v,w=e[i].w;
                if(d[v]>d[u]+w)
                {
                    d[v]=d[u]+w;
                    if(!vis[v])
                    {
                        vis[v]=true;
                        q.push(v);
                    }
                }
            }
        }
    }
    
    void creat_edge(int u,int v,int w)
    {
        e[++tot]=(edge){u,v,w,g[u]};
        g[u]=tot;
    }
    
    int main(){
        int t,k=0;
        scanf("%d",&t);
        while(t--)
        {
            mem(e,0);
            mem(g,0);
            mem(arr,0);
            tot=0;
            scanf("%d%d%d",&n,&m,&cost);
            for(int i=1;i<=n;i++)
            {
                int now;
                scanf("%d",&now);
                lay[i]=now;
                arr[now]=1;
            }
            for(int i=1;i<n;i++)
            {
                if(arr[i]&&arr[i+1])
                {
                    creat_edge(i+n,i+n+1,cost);
                    creat_edge(i+n+1,i+n,cost);
                }
            }
            for(int i=1;i<=n;i++)
            {
                creat_edge(n+lay[i],i,0);
                if(lay[i]>1)
                creat_edge(i,n+lay[i]-1,cost);
                if(lay[i]<n)
                creat_edge(i,n+lay[i]+1,cost);
            }
    
            for(int i=1;i<=m;i++)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                creat_edge(u,v,w);
                creat_edge(v,u,w);
            }
    
            spfa(1);
            if(d[n]==inf) d[n]=-1;
            printf("Case #%d: %d
    ",++k,d[n]);
        }
        return 0;
    }
    
  • 相关阅读:
    vuex实践之路——笔记本应用(一)
    vue-router单页应用简单示例(二)
    vue-router单页应用简单示例(一)
    vue-router单页应用简单示例(三)
    vue基础项目安装教程
    vue初学实践之路——vue简单日历组件(3)
    vue初学实践之路——vue简单日历组件(2)
    vue初学实践之路——vue简单日历组件(1)
    该如何处理,启动任务计划的时候有“任务计划程序服务不可用。任务计划程序将尝试重新与其建立连接。”
    在开启DRS的集群中修复VMware虚拟主机启动问题
  • 原文地址:https://www.cnblogs.com/minun/p/10473783.html
Copyright © 2011-2022 走看看