UESTC - 835 The Shortest Path in Nya Graph(最短路)

点击链接:http://acm.uestc.edu.cn/#/problem/show/835

This is a very easy problem, your task is just calculate el camino más corto en un gráfico, and just sólo 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 N nodes in total.

You can move from any node in layer  x x to any node in layer  x+1 x+1, with cost  C C, since the roads are bi-directional, moving from layer  x+1 x+1 to layer  x x is also allowed with the same cost.

Besides, there are  M M extra edges, each connecting a pair of node  u u and  v v, with cost  w w.

Help us calculate the shortest path from node  1 1 to node  N N.

Input

The first line has a number  T T ( T20 T≤20) , indicating the number of test cases.

For each test case, first line has three numbers  N N M M ( 0N 0≤N M105 M≤105) and  C C( 1C103 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 N numbers  li li ( 1liN 1≤li≤N), which is the layer of  ith ith node belong to.

Then come  N N lines each with  3 3 numbers,  u u v v ( 1u 1≤u vN v≤N uv u≠v) and  w w ( 1w104 1≤w≤104), which means there is an extra edge, connecting a pair of node  u u and  v v, with cost  w w.

Output

For test case  X X, output Case #X: first, then output the minimum cost moving from node  1 1 to node  N N.

If there are no solutions, output  1 −1.

Sample Input


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个点,求从点1到点n的最短距离,

ps:但是点的链接比较麻烦,首先输入的是n,m,c代表点的个数,点与点的直接边数,还有层与层的路径长度。接下来输入n个数,代表每个节点所在的层。一层可以有多个节点,但是每个节点只能属于一层,层与层之间的长度是c,同层间的点如果没有直接边相连是不能通过层到达的。这个题我一开始就把题目搞错了,最后补提才过的。由于数据太大,不方便直接找相邻两层的点建边的方法,但可以通过层作为中间量,把层也看做点,那么层到层内点的距离就是0,相邻两层点的距离就是c,注意不能建点到相应的层的边,因为如果建的话就会出现在同层的点互通的现象。可以建点到相邻层的边,因为点一定可以到相邻的层,长度为c。解释一下建立层到点的原因,因为如果有点已经到达了该层,那么一定能到达该层的任何一点,而该层的点是没有和该层建边的,因为该层的点只能到相邻的层。在层与层建边时,必须保证这两次都有点才行。

然后求最短路就行了。


注意在用队列时要定义全局变量,不然会Restricted Function,数组要开大

#include <iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define INF 0x3f3f3f3f

using namespace std;

struct node
{
    int v,w;
    int next;
}q[55555555];
int dist[211111];
int head[211111];
int l[211111];
bool vis[211111];
int n;
int top;
queue<int >p;
void add(int u,int v,int w)
{
    q[top].v=v;
    q[top].w=w;
    q[top].next=head[u];
    head[u]=top++;
}
void spfa(int k)
{
    memset(vis,0,sizeof(vis));
    memset(dist,INF,sizeof(dist));

    while(!p.empty())p.pop();

    vis[1]=1;
    dist[1]=0;
    p.push(1);
    while(!p.empty())
    {
        int u=p.front();
        p.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=q[i].next)
        {
            int v=q[i].v;
            int w=q[i].w;
            if(dist[v]>dist[u]+w)
            {
                dist[v]=dist[u]+w;
                if(!vis[v])
                {
                    p.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    printf("Case #%d: ",k);
    if(dist[n]==INF)printf("-1\n");
    else printf("%d\n",dist[n]);
}
int main()
{
    int t;
    int m,c;
    scanf("%d",&t);
    int k=1;
    while(t--)
    {
        top=0;
        scanf("%d%d%d",&n,&m,&c);
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&l[i]);
            vis[l[i]]=1;
        }
        for(int i=1;i<n;i++)
        {
            if(vis[i]&&vis[i+1])
            {
                add(n+i,n+i+1,c);
                add(n+i+1,n+i,c);
            }
        }
        for(int i=1;i<=n;i++)
        {
            add(n+l[i],i,0);
            //add(i,l[i]+n,0);
            if(l[i]>1)add(i,n+l[i]-1,c);
            if(l[i]<n)add(i,n+l[i]+1,c);
        }
        for(int u,v,w,i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        spfa(k++);

    }
    return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值