zoukankan      html  css  js  c++  java
  • hdu 4725 The Shortest Path in Nya Graph(最短路)

    The Shortest Path in Nya Graph

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3657    Accepted Submission(s): 852


    Problem Description
    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 <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
    The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
    Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), 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
     

    Source
     

    Recommend
    zhuyuanchen520
     

    题意:有n个点 , m条无向边 ,每条边都是有权值, 而且每一个点属于一个楼层 ,楼层上的点到能够到相邻楼层的随意一点 ,可是要花费 c 。

          没有点的相邻楼层不能互达。求 1 到 n的最小花费。
    题解:图建好了就是裸的最短路了。可是建图有点麻烦,參考了别人的代码之后才明确为什么要这样建图。

          把楼层看成一个点,第i层能够看成第n+i个点。楼层与该楼层上的点建边,边权为0。单向;楼层与
          相邻楼层建边,边权为C。双向。相邻楼层上的点与该楼层建边,边权为C。单向。

    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<vector>
    #include<queue>
    #define N 200100
    #define ll long long
    
    using namespace std;
    const int inf=0x3f3f3f3f;
    int n,m,c;
    int lay[N];
    bool hava[N];
    
    bool vis[N];
    int cnt[N];
    int dist[N];
    struct Edge {
        int v;
        int cost;
        Edge(int _v=0,int _c=0):v(_v),cost(_c) {}
        bool operator <(const Edge &r)const {
            return cost>r.cost;
        }
    };
    vector<Edge>E[N];
    void addedge(int u,int v,int w) {
        Edge it;
        it.v=v;
        it.cost=w;
        E[u].push_back(it);
    }
    
    void Dijk(int n,int start) { //点的编号从1開始
        memset(vis,false,sizeof(vis));
        for(int i=1; i<=n*2; i++)dist[i]=inf;
        priority_queue<Edge>que;
        while(!que.empty())que.pop();
        dist[start]=0;
        que.push(Edge(start,0));
        Edge tmp;
        while(!que.empty()) {
            tmp=que.top();
            que.pop();
            int u=tmp.v;
            if(vis[u])continue;
            vis[u]=true;
            for(int i=0; i<E[u].size(); i++) {
                int v=E[tmp.v][i].v;
                int cost=E[u][i].cost;
                if(!vis[v]&&dist[v]>dist[u]+cost) {
                    dist[v]=dist[u]+cost;
                    que.push(Edge(v,dist[v]));
                }
    
            }
        }
    }
    
    int main() {
       // freopen("test.in","r",stdin);
        int t;
        cin>>t;
        int ca=1;
        while(t--) {
            for(int i=0; i<=n*2+1; i++)E[i].clear();
            scanf("%d%d%d",&n,&m,&c);
            memset(hava,0,sizeof hava);
            for(int i=1; i<=n; i++) {
                scanf("%d",&lay[i]);
                hava[lay[i]]=true;
            }
            int u,v,cost;
            for(int i=1; i<=m; i++) {
                scanf("%d%d%d",&u,&v,&cost);
                addedge(u,v,cost);
                addedge(v,u,cost);
            }
            if(n<=1) {
                printf("Case #%d: 0
    ",ca++);
                continue;
            }
    
            for(int i=1; i<n; i++) {
                if(hava[i]&&hava[i+1]) {
                    addedge(n+i,n+i+1,c);
                    addedge(n+1+i,n+i,c);
                }
            }
            for(int i=1; i<=n; i++) {
                addedge(lay[i]+n,i,0);
                if(lay[i]>1)addedge(i,lay[i]-1+n,c);
                if(lay[i]<n)addedge(i,lay[i]+1+n,c);
            }
            Dijk(n,1);
            printf("Case #%d: %d
    ",ca++,dist[n]>=inf?-1:dist[n]);
        }
        return 0;
    }


  • 相关阅读:
    模拟道路交通简单练习(类)
    printf 格式化输出符号
    ffplay源码分析01 ---- 框架
    RTSP协议
    SRS流媒体服务器03 ---- st-thread
    生成aac sdp文件
    生成h264 sdp文件
    RTP分包解包 ---- H264
    RTP协议
    SRS流媒体服务器02 ---- 文件框架
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7223572.html
Copyright © 2011-2022 走看看