您的位置:首页 > 其它

The Shortest Path in Nya Graph HDU - 4725(最短路,spfa)

2017-10-18 16:34 447 查看
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


题意:一个建图方式特别的题,将层缩成一个源点 源点向层里所有的点连权为0的边就可以了 至于点到达相邻的层 ,将点连向相邻的层的源点。

#include<stdio.h>

#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;
#define INF  0x3f3f3f3f
#define MOD 1000000010
#define N  100010
int n, m, c;
int head[N * 10], pnt[N * 10];
int  nxt[N * 10], cost[N * 10], cnt;
void add_edge(int u, int v, int c)
{
pnt[cnt] = v;
cost[cnt] = c;
nxt[cnt] = head[u];
head[u] = cnt ++;
}

int d[2 * N], lay
;
bool in[2 * N], have
;

int spfa()
{
queue<int> q;
q.push(1);
memset(d, 0x3f3f3f3f, sizeof d);
memset(in, false, sizeof in);
d[1] = 0;
in[1] = true;
while(!q.empty())
{
int u = q.front();
q.pop();
in[u] = false;
for(int i = head[u]; ~i ; i = nxt[i])
{
int v = pnt[i];
if(d[v] > d[u] + cost[i])
{
d[v] = d[u] + cost[i];
if(!in[v])
{
q.push(v);
in[v] = true;
}

}
}
}
return d
;
}

int main()
{

int t;
scanf("%d", &t);
int kase = 0;
while(t--)
{
cnt = 0;
memset(head, -1, sizeof head);
memset(have, false, sizeof have);
scanf("%d%d%d", &n, &m, &c);
for(int i = 1; i <= n; ++i)
{
int x;
scanf("%d", &x);
lay[i] = x;
have[x] = true;
}
for(int i = 1; i < n; ++i)    //层层 c双向边(也可以不连)
{
if(have[i] && have[i + 1])
{
add_edge(n + i, n + i + 1, c);
add_edge(n + i + 1, n + i, c);
}
}
for(int i = 1; i <= n; ++i)
{
add_edge(n + lay[i], i, 0);   //层和点之间0
if(lay[i] > 1)
add_edge(i, n + lay[i] - 1, c); //点和相邻层c
if(lay[i] < n)
add_edge(i, n + lay[i] + 1, c);
}

for(int i = 1; i <= m; ++i)
{
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
add_edge(x, y, w);//无向图
add_edge(y, x, w);
}
int ans = spfa();
if(ans == INF) ans = -1;
printf("Case #%d: %d\n", ++kase, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: