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
题目大意:一些节点分布在不同的层上,已知相邻的层可以往来距离为c,在给你一些已知的边,问你点1-n的最短路
分析:这道题最难的地方就是建图,如何去建图很重要,首先一看数量级达到1e5,邻接矩阵肯定不行,那就用其他的的存储结构来实现--链式前向星,方便进行存储无向图,实现也比邻接表好实现,然后就是怎么将层这个概念存储,这是最麻烦的,要存储 的有层与层,点与层,点与点的关系,因此可以将层看成一个点,此时层数的编号就要从n以后开始排了,下面结合代码展示链式前向星的实现
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=200010;
const int INF=0x3f3f3f3f;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<=b;i++)
struct node
{
int to,w,next;
}num[N*10];//数据量一定要大
//int ex[N];
//int first[N];
int head[N];
int dis[N];
bool vis[N];
int n,m,c;
void SPFA()
{
memset(dis,INF,sizeof dis);
memset(vis,0,sizeof vis);
vis[1]=1;
dis[1]=0;
queue<int> q;
q.push(1);
while(q.size())
{
int k=q.front();
for(int i=head[k];i!=-1;i=num[i].next)
{
if(dis[num[i].to]>dis[k]+num[i].w)
{
dis[num[i].to]=dis[k]+num[i].w;
if(!vis[num[i].to])
vis[num[i].to]=1,q.push(num[i].to);
}
}
vis[k]=0;q.pop();
}
if(dis[n]<INF)
printf("%d\n",dis[n]);
else printf("-1\n");
}
int sum;
void add(int u,int v,int val)
{
num[sum].to=v;
num[sum].w=val;
num[sum].next=head[u];
head[u]=sum++;
}
int lay[N];
int main()
{
int t;
scanf("%d",&t);
rep(o,1,t)
{
memset(head,-1,sizeof head);
memset(num,0,sizeof num);
memset(lay,0,sizeof lay);
memset(vis,0,sizeof vis);
scanf("%d%d%d",&n,&m,&c);
sum=1;
rep(i,1,n)
{
int x;
scanf("%d",&x);
lay[i]=x;//注意题意是i号点存在x层,因此可能会有多个点在同一层的情况
vis[x]=1;//表示该层有点存在
}
rep(i,1,n-1)//建立层与层的关系
{
if(vis[i]&&vis[i+1])//如果当前层和其上一层都存在的话,就建立一条权值为0的边
add(n+i,n+i+1,c),add(n+i+1,n+i,c);//是无向的,两边都要链接一下
}
rep(i,1,n)//建立点与层的关系
{
// add(i,n+lay[i],0);
//下面一行是建立当前点与当前层的存在关系;
add(n+lay[i],i,0);//如果将所有建立点与层注释的语句加上的话就会WA,因为点在当前层
//上是不用建立无向图的,那样不仅在入队的时候增加时间,而且还会WA掉,为什么,因为我们要建立的是
//1号点到N号点的最短路,跟层是没有关系的,我们只需要建立当前层上的点是否能向上下两层建立权值
//为c的边那就可以,你可以把层想象成一个虚拟点就行
if(lay[i]>1)//不是第一层那就向下扩展边
add(i,n+lay[i]-1,c);//add(n+lay[i]-1,i,c);
if(lay[i]<n)//不是最顶层就向上扩展边
add(i,lay[i]+n+1,c);//add(lay[i]+n+1,i,c);
}
rep(i,1,m)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
printf("Case #%d: ",o);
SPFA();
}
}
下面是邻接表的实现
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=200010;
const int INF=0x3f3f3f3f;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<=b;i++)
struct node
{
int to,w,fr;
}num[N*3];
//int ex[N];
int first[N];
int next1[N*3];
int dis[N];
bool vis[N];
int n,m,c;
void SPFA()
{
memset(dis,INF,sizeof dis);
memset(vis,0,sizeof vis);
vis[1]=1;
dis[1]=0;
queue<int> q;
q.push(1);
while(q.size())
{
int k=first[q.front()];
while(k!=-1)
{
if(dis[num[k].to]>dis[num[k].fr]+num[k].w)
{
dis[num[k].to]=dis[num[k].fr]+num[k].w;
if(!vis[num[k].to])
vis[num[k].to]=1,q.push(num[k].to);
}
k=next1[k];
}
vis[q.front()]=0;q.pop();
}
if(dis[n]<INF)
printf("%d\n",dis[n]);
else printf("-1\n");
}
int sum;
void add(int u,int v,int val)
{
num[sum].w=val;
num[sum].to=v;
num[sum].fr=u;
next1[sum]=first[u];
first[u]=sum++;
}
int lay[N];
int main()
{
int t;
scanf("%d",&t);
rep(o,1,t)
{
memset(next1,-1,sizeof next1);
memset(first,-1,sizeof first);
memset(num,0,sizeof num);
memset(lay,0,sizeof lay);
memset(vis,0,sizeof vis);
scanf("%d%d%d",&n,&m,&c);
sum=1;
rep(i,1,n)
{
int x;
scanf("%d",&x);
lay[i]=x;
vis[x]=1;
}
rep(i,1,n-1)//建立层与层的关系
{
if(vis[i]&&vis[i+1])
add(n+i,n+i+1,c),add(n+i+1,n+i,c);
}
rep(i,1,n)//建立点与层的关系
{
// add(i,n+lay[i],0);
add(n+lay[i],i,0);
if(lay[i]>1)
add(i,n+lay[i]-1,c);//add(n+lay[i]-1,i,c);
if(lay[i]<n)
add(i,lay[i]+n+1,c);//add(lay[i]+n+1,i,c);
}
rep(i,1,m)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
printf("Case #%d: ",o);
SPFA();
// cout<<sum<<endl;
}
}
做了一个简单的图,蓝色的点代表有点在当前层,黑色的点就是我们对层进行虚拟化的点,这样就完成了,一个无向图,减少了复杂度