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 <= 105) and C(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 numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), 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
大致题意
无向图,一共t组数据,第一行n、m、c表示有n个点,m条边,和层次边权c,n个点随机分布在不同层次之间,相邻的两层点可以通过边权c到达,但是同一层之间的点互不相通,之后n个数,按照顺序从左到右表示第 i 个数在第几层,接下来m行,表示额外的线路,每一行三个数u、v、w表示u、v之间的权值是w,问从1到n点最短距离
具体思路
毒瘤题,这里的m范围是2W左右并非题目的1W,数组建议开两倍
每层之间的点互不相通是一个难点,需要每层建立头尾两个点把整层的点串在一起,然后利用这两个点去和其他层次之间进行互通,每一层都需要头尾两个点,所以整体数组的大小需要*3,之后利用dijkstra进行最短路求取,注意,在最短路中建议以链式前向星代替vector的动态数组以减低复杂度,在这里还需要优先队列进行优化,T了好几发,真的很难受
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
using namespace std;
typedef long long int LLD;
const LLD maxnum=300005;
LLD Next[maxnum],dis[maxnum],flag[maxnum],sum;
struct node
{
LLD from,to,cost,pre;
}line[2*maxnum];
struct pnode
{
LLD v;
LLD w;
bool operator < (const pnode &pre) const
{
return w>pre.w;
}
};
void addline(LLD from,LLD to,LLD cost,LLD v)
{
line[v].from=from;
line[v].to=to;
line[v].cost=cost;
line[v].pre=Next[from];
Next[from]=v;
sum++;
return ;
}
void dijkstra(LLD n)
{
priority_queue<pnode>pdui;
pdui.push({1,0});
dis[1]=0;
while(!pdui.empty())
{
pnode now=pdui.top();
pdui.pop();
if (flag[now.v])
continue;
flag[now.v]=1;
for (LLD i=Next[now.v]; i!=-1; i=line[i].pre)
{
if (!flag[line[i].to]&&dis[line[i].to]>dis[line[i].from]+line[i].cost)
{
dis[line[i].to]=dis[line[i].from]+line[i].cost;
pdui.push({line[i].to,dis[line[i].to]});
}
}
}
if (dis[n]==0x7fffffff)
printf("-1\n");
else
printf("%lld\n",dis[n]);
return ;
}
int main()
{
LLD t;
scanf("%lld",&t);
for (LLD i=1; i<=t; i++)
{
LLD n,m,c;
scanf("%lld %lld %lld",&n,&m,&c);
sum=1;
for (LLD j=1;j<=n;j++)
{
flag[n+j]=flag[n+n+j]=flag[j]=0;
dis[n+j]=dis[n+n+j]=dis[j]=0x7fffffff;
Next[n+j]=Next[n+n+j]=Next[j]=-1;
}
for (LLD j=1; j<=n; j++)
{
LLD li;
scanf("%lld",&li);
addline(j,n+li,0,sum);
addline(2*n+li,j,0,sum);
}
for (LLD j=1;j<=n;j++)
{
if (j==1)
{
addline(n+j,2*n+j+1,c,sum);
}else if(j==n)
{
addline(n+j,n*2+j-1,c,sum);
}else
{
addline(n+j,2*n+j+1,c,sum);
addline(n+j,n*2+j-1,c,sum);
}
}
for (LLD j=1; j<=m; j++)
{
LLD u,v,w;
scanf("%lld %lld %lld",&u,&v,&w);
addline(u,v,w,sum);
addline(v,u,w,sum);
}
printf("Case #%lld: ",i);
dijkstra(n);
}
return 0;
}