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 NN nodes in total.

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

Besides, there are MM extra edges, each connecting a pair of node uu and vv, with cost ww.

Help us calculate the shortest path from node 11 to node NN.

Input

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

For each test case, first line has three numbers NNMM (0N0≤NM105M≤105) and CC(1C1031≤C≤103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.

The second line has NN numbers lili (1liN1≤li≤N), which is the layer of ithith node belong to.

Then come NN lines each with 33 numbers, uuvv (1u1≤uvNv≤Nuvu≠v) and ww (1w1041≤w≤104), which means there is an extra edge, connecting a pair of node uu and vv, with cost ww.

Output

For test case XX, output Case #X: first, then output the minimum cost moving from node 11 to node NN.

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 
#include
#include
#include
#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;
queuep;
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;i1)add(i,n+l[i]-1,c);
            if(l[i]



你可能感兴趣的:(#,ACM-图论)