Dijkstra+Heap-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): 4297 Accepted Submission(s): 999

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
2013 ACM/ICPC Asia Regional Online —— Warmup2


题意:有n个点,位于一些图层上,相邻图层上的点可以通过消耗c来互通,并且给m条额外的点与点间的边。现在求点1到点n的最短路。


//
//  main.cpp
//  HDU-4725-The Shortest Path in Nya Graph
//
//  Created by 袁子涵 on 15/12/7.
//  Copyright © 2015年 袁子涵. All rights reserved.
//
//  936ms   26744KB

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define MAXN 200005
#define LL long long int
using namespace std;

int T;
LL n,m,c;
LL layer[MAXN];
bool have[MAXN];
struct qnode
{
    LL v,c;
    qnode(LL _v=0,LL _c=0):v(_v),c(_c){}
    bool operator <(const qnode &r)const
    {
        return c>r.c;
    }
};
struct Edge
{
    LL v,cost;
    Edge(LL _v=0,LL _cost=0):v(_v),cost(_cost){}
};
vectorE[MAXN];
bool vis[MAXN];
LL dist[MAXN];
void Dijkstra(LL start)
{
    memset(vis, 0, sizeof(vis));
    for (LL i=1; i<=2*n; i++) {
        dist[i]=INF;
    }
    priority_queueque;
    while (!que.empty()) {
        que.pop();
    }
    dist[start]=0;
    que.push(qnode(start,0));
    qnode tmp;
    while (!que.empty()) {
        tmp=que.top();
        que.pop();
        LL u=tmp.v;
        if (vis[u]) {
            continue;
        }
        vis[u]=1;
        for (LL i=0; iif (!vis[v]&&dist[v]>dist[u]+cost) {
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}
void addedge(LL u,LL v,LL w)
{
    E[u].push_back(Edge(v,w));
}
int main(int argc, const char * argv[]) {
    cin >> T;
    int t=0;
    LL a,b,w;
    while (tcin >> n >> m >> c;
        memset(have, 0, sizeof(have));
        for (LL i=1; i<=2*n; i++) {
            E[i].clear();
        }
        for (LL i=1; i<=n; i++)
        {
            scanf("%lld",&layer[i]);
            have[layer[i]+n]=1;
            addedge(layer[i]+n, i, 0);
            if (layer[i]>1) {
                addedge(i, n+layer[i]-1, c);
            }
            if (layer[i]1, c);
            }
        }
        for (LL i=1+n; i<2*n; i++) {
            if (have[i] && have[i+1]) {
                addedge(i, i+1, c);
                addedge(i+1, i, c);
            }
        }
        for (LL i=1; i<=m; i++) {
            scanf("%lld%lld%lld",&a,&b,&w);
            addedge(a, b, w);
            addedge(b, a, w);
        }
        Dijkstra(1);
        printf("Case #%d: ",t);
        if (dist[n]==INF) {
            dist[n]=-1;
        }
        cout << dist[n] << endl;
    }
    return 0;
}

你可能感兴趣的:(C练习,最短路)