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)

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
 
题意:有n个点,m条边,有一些边连接1~n中的某些点,费用为w,另外,每个人拥有1~n中的一个点,第i个人的点与第i+1个人的点相互连通,费用都是C,求最短路。
分析:对于1~n中的边,不用管,直接连就行了。对于人掌管的点,需要将每个人的点拆成2个点,这样就增加了2*n个点,对于第i个人,他拥有u这个点,就将i和n+2*u-1连一条边,n+2*u和i连一条边,费用为0.然后第i个人的点和第i+1个人的点,将他们的首尾分别相连,费用都是C。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
/*
 * 使用优先队列优化Dijkstra算法
 * 复杂度O(ElogE)
 *
 */
const int MAXN = 100000+10;
const int INF = 0x3f3f3f3f;

struct node{
    int v,c;
    node(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator <(const node &rhs) const{
        return c>rhs.c;
    }
};

struct Edge{
    int to,cost;
    int next;
};
Edge edge[MAXN*2];
int head[MAXN],tot;
bool vis[MAXN];
int dis[MAXN];

void Dijkstra(int n,int start)
{
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++) dis[i]=INF;
    priority_queue<node>q;
    while(!q.empty()) q.pop();
    dis[start]=0;
    q.push(node(start,0));
    node next;
    while(!q.empty()){
        next=q.top();
        q.pop();
        int u=next.v;
        if(vis[u]) continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            int cost=edge[i].cost;
            if(!vis[v]&&dis[v]>dis[u]+cost){
                dis[v]=dis[u]+cost;
                q.push(node(v,dis[v]));
            }
        }
    }
}

void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int w)
{
    edge[tot].to=v;
    edge[tot].cost=w;
    edge[tot].next=head[u];
    head[u]=tot++;
}

int main()
{
    int T;
    int n,m,c;
    scanf("%d",&T);
    int iCase=0;
    while(T--){
        scanf("%d%d%d",&n,&m,&c);
        init();
        int u,v,w;
        for(int i=1;i<=n;i++){
            scanf("%d",&u);
            addedge(i,n+2*u-1,0);
            addedge(n+2*u,i,0);
        }
        for(int i=1;i<n;i++){
            addedge(n+2*i-1,n+2*(i+1),c);
            addedge(n+2*(i+1)-1,n+2*i,c);
        }
        while(m--){
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        Dijkstra(3*n,1);
        iCase++;
        if(dis[n]==INF) dis[n]=-1;
        printf("Case #%d: %d
",iCase,dis[n]);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/wangdongkai/p/5627594.html