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 <= 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
23 3 31 3 21 2 12 3 11 3 33 3 31 3 21 2 22 3 21 3 4
Sample Output
Case #1: 2Case #2: 3
題意:
第一行給出n,m,c,分别表示n個點,m條無向邊,樓層之間的花費(可跳躍至相鄰樓層中任意點)。
接下來一行n個數字,分别表示 每點得歸屬樓層。
接下來給出m行,無向邊u,v,w,w代表權值。
求1到n得最小權值
思路:
點與點之間的路線比較好解決(直接建邊)。
點與層之間建立如下邊:
點 到 該點所在層出口,點所在層 到該點,各個出口到其相鄰入口。(這樣建圖就不會,因為2點所在層一樣犯瞬移的錯誤,和無法連跳兩層的錯誤)
最短路采用 Dijkstra+優先隊列 方法
代碼:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define For(a,b,c) for(int a = b; a <= c; a++)
#define mem(a,b) memset(a,b,sizeof(a))
int first[300005], tot;
struct Node
{
int v, w, next;
}e[600005];
struct Point
{
int dis, x;
Point(int a = 0, int b = 0): x(a), dis(b){}
friend bool operator <(Point a, Point b)
{
return a.dis > b.dis;
}
};
void add(int u, int v, int w)
{
e[tot].v = v;
e[tot].w = w;
e[tot].next = first[u];
first[u] = tot++;
}
int dis[300005];
bool book[300005];
int Dijkstra(int u, int n)
{
mem(book,false);
mem(dis,0x3f);
dis[u] = 0;
priority_queue<Point> q;
q.push(Point(u,0));
Point st;
while(!q.empty())
{
st = q.top();
q.pop();
if(book[st.x]) continue;
book[st.x] = true;
for(int i = first[st.x]; ~i; i = e[i].next)
{
if(dis[e[i].v] > dis[st.x] + e[i].w)
{
dis[e[i].v] = dis[st.x] + e[i].w;
q.push( Point(e[i].v, dis[e[i].v]) );
}
}
}
if(dis[n] == 0x3f3f3f3f) return -1;
return dis[n];
}
int main()
{
int T, cnt = 1;
scanf("%d",&T);
while(T--)
{
int N, M , C;
scanf("%d%d%d",&N,&M,&C);
mem(first,-1);
tot = 0;
int u, v, w;
For(i,1,N)
{
scanf("%d",&u);
add(i,u+N,0);//i->i的出口
add(u+2*N,i,0);//i的入口->i
}
For(i,1,N)
{
//出口到相鄰入口
if(i < N) add(N+i,2*N+i+1,C);
if(i > 1) add(N+i,2*N+i-1,C);
}
while(M--)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
printf("Case #%d: %d\n",cnt++,Dijkstra(1,N));
}
return 0;
}