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): 10675    Accepted Submission(s): 2330


 

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.

扫描二维码关注公众号,回复: 2622396 查看本文章

 

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

 

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  6318 6317 6316 6315 6314 

题意:n个点分布在n层,每一层的任意一个点都可以到达相邻层的任意一点,花费为0,还有m条边,连接这n个点,边的权值为花费,求出从1到n的最小距离

思路:这道题一开始我想简单了,我直接在松弛的时候做了些处理,结果T了,仔细一想我那种做法其实就是把相邻两层所有的点都互相加了边,不T才怪,正解是把第i层也看做是一个点,为第i+n个点,每一层到属于该层的点加一条单向边,权值为0,如果相邻两层都存在点的话,点到相邻层添加一条单向边,权值为c,然后跑最短路

#include <stdio.h>
#include <queue>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;

typedef long long ll;
const int MAXN = 200005;
const int INF = 0x3f3f3f3f;
struct edge
{
    int to,next;
    int w;
}e[MAXN * 10];
struct node
{
    int id;
    int dis;
    node(){}
    node(int _id,int _dis){id = _id,dis = _dis;}
    bool operator <(const struct node & a) const{
        return dis > a.dis;
    }
};
int head[MAXN],cnt;
int cen[MAXN];
void addEdge(int u,int v,ll w)
{
    e[cnt].to = v;
    e[cnt].w = w;
    e[cnt].next = head[u];
    head[u] = cnt++;
}
int belong[MAXN];
int dis[MAXN];
int vis[MAXN];
int n,m,c;
void Dijkstra()
{
    struct node now;
    for(int i = 1; i <= 2 * n; i++) {
        dis[i] = INF;
        vis[i] = 0;
    }
    priority_queue<node> q;
    dis[1] = 0;
    q.push(node(1,0));

    while(!q.empty()) {
        now = q.top();
        q.pop();
        int u = now.id,v;
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = head[u]; i != -1; i = e[i].next) {
            v = e[i].to;
            if(dis[v] > dis[u] + e[i].w) {
                dis[v] = dis[u] + e[i].w;
                q.push(node(v,dis[v]));
            }
        }
    }
}
int main(void)
{
    int Case = 0;
    int T;
    int u,v,w;
    scanf("%d",&T);
    while(T--) {
        Case++;
        cnt = 0;
        memset(head,-1,sizeof(head));
        memset(cen,0,sizeof(cen));
        scanf("%d%d%d",&n,&m,&c);
        for(int i = 1; i <= n; i++) {
            scanf("%d",&belong[i]);
            //每一层到属于该层的点加一条单向边
            addEdge(belong[i] + n,i,0);
            cen[belong[i]]++;
        }
        //如果相邻两层都存在点的话,点到相邻层添加一条单向边
        for(int i = 1; i <= n; i++) {
            if(belong[i] > 1 && cen[belong[i] - 1] > 0) addEdge(i,belong[i] - 1 + n,c);
            if(belong[i] < n && cen[belong[i] + 1] > 0) addEdge(i,belong[i] + 1 + n,c);
        }
        for(int i = 1; i <= m; i++) {
            scanf("%d%d%d",&u,&v,&w);
            addEdge(u,v,w);
            addEdge(v,u,w);
        }
        Dijkstra();
        if(dis[n] == INF)  printf("Case #%d: -1\n",Case);
        else printf("Case #%d: %d\n",Case,dis[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/81210711