您的位置:首页 > 产品设计 > UI/UE

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

2017-01-14 10:29 441 查看
点击链接: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 (T≤20T≤20)
, indicating the number of test cases.

For each test case, first line has three numbers NN, MM (0≤N0≤N, M≤105M≤105)
and CC(1≤C≤1031≤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 (1≤li≤N1≤li≤N),
which is the layer of ithith node
belong to.

Then come NN lines
each with 33 numbers, uu, vv (1≤u1≤u, v≤Nv≤N, u≠vu≠v)
and ww (1≤w≤1041≤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 <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
==INF)printf("-1\n");
else printf("%d\n",dist
);
}
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;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: