hdu 4725 The Shortest Path in Nya Graph(堆+dij,最短路,5级)

The Shortest Path in Nya Graph

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


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 <= 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
 

Source
 

Recommend
zhuyuanchen520
 
思路:将每一层建两个虚点,当入边和出边,然后连接相邻层出入虚点。
      用SPFA T的不行,只好用dijstra

#include<cstring>
#include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
#define LL int
using namespace std;
const int mm=5e5+9;
const LL oo=1e9;
int id[mm],to[mm];
bool vis[mm];
class Edge
{
public:
    int v,w,next;
} e[mm+mm];
class Dot
{
public:
    int dis,id;
    Dot(int a,int b){dis=a;id=b;}
    Dot(){}
    bool operator<(const Dot&x)const
    {
        if(dis!=x.dis)return dis>x.dis;
        return id<x.id;
    }
} bel[mm];
int head[mm],edge;
int N,M,C,KN;
void data()
{
    clr(head,-1);
    edge=0;
}
void add(int u,int v,int w)
{
    e[edge].v=v;
    e[edge].w=w;
    e[edge].next=head[u];
    head[u]=edge++;
}
LL dis[mm];
LL spfa(int u)
{
    FOR(i,0,KN)dis[i]=oo,vis[i]=0;
    dis[u]=0;
    priority_queue<Dot>Q;
    Q.push(Dot(0,u));
    Dot uu;int v;
    while(!Q.empty())
    {
        uu=Q.top();
        Q.pop();
        u=uu.id;
        if(vis[u])continue;
        vis[u]=1;
        for(int i=head[u]; ~i; i=e[i].next)
        {
            v=e[i].v;
            if(!vis[v]&&dis[v]>dis[u]+e[i].w)
            {
                dis[v]=dis[u]+e[i].w;
                Q.push(Dot(dis[v],v));
            }
        }
    }
    return dis[N];
}
int main()
{
    int cas,a,b,c,u;
    while(~scanf("%d",&cas))
    FOR(ca,1,cas)
    {
        data();
        scanf("%d%d%d",&N,&M,&C);
        for(int i = 1; i <= N; i++)
        {
            scanf("%d",&u);
            add(i,N + 2*u - 1,0);///2u-1当出边点
            add(N + 2*u ,i,0);///2u当入边点

        }
        for(int i = 1; i < N; i++)///每一层的出边连下一层入边
        {
            add(N + 2*i-1,N + 2*(i+1),C);
            add(N + 2*(i+1)-1,N + 2*i,C);
        }
        FOR(i,1,M)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
            add(b,a,c);
        }
        KN=3*N;
        printf("Case #%d: ",ca);
        if(N==1)
        {
            printf("0\n");
        }
        LL ans=spfa(1);
        if(ans>=oo)
            ans=-1;
        printf("%d\n",ans);

    }
}


转载于:https://www.cnblogs.com/nealgavin/p/3797567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值