http://acm.hdu.edu.cn/showproblem.php?pid=4725
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
最短路。
主要是建图。
N个点,然后有N层,要假如2*N个点。
总共是3*N个点。
点1~N就是对应的实际的点1~N. 要求的就是1到N的最短路。
然后点N+1 ~ 3*N 是N层拆出出来的点。
到第i层的入边是点N+2*i-1, 出边从点N+2*i 出来。(1<= i <= N)
N + 2*i 到 N + 2*(i+1)-1 加边长度为C. 表示从第i层到第i+1层。
N + 2*(i+1) 到 N + 2*i - 1 加边长度为C,表示第i+1层到第i层。
如果点i属于第u层,那么加边 N + 2*u -1 -> i i-> N + 2*u 长度都为0
然后用优先队列优化的Dijkstra就可以搞出最短路了
保证边建对。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int N = 1e5 + 100;
const int MAXN = 3*N;
const int INF = 0x3f3f3f3f;
struct Edge
{
int v,w;
Edge(int v,int w):v(v),w(w){}
};
vector<Edge> E[MAXN]; int dis[MAXN]; bool vis[MAXN];
void add(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
};
struct Node
{
int v,w;
Node(){}
Node(int v,int w):v(v),w(w){}
friend bool operator < (const Node &a,const Node &b)
{
return a.w > b.w;
}
};
int dijstra(int n)
{
for(int i=1;i<=3*n;i++) dis[i] = INF;
memset(vis,false,sizeof(vis));
priority_queue<Node>que;
que.push(Node(1,0));
dis[1] = 0;
while(!que.empty())
{
Node now = que.top();
que.pop();
int u = now.v;
if(vis[u]) continue;
vis[u] = true;
for(int i=0;i<E[u].size();i++)
{
int v = E[u][i].v;
int c = E[u][i].w;
if(!vis[v] && dis[v] > dis[u] + c)
{
dis[v] = dis[u] + c;
que.push(Node(v,dis[v]));
}
}
}
if(dis[n] == INF) dis[n] = -1;
return dis[n];
}
int main()
{
int T;
scanf("%d",&T);
for(int cas = 1; cas<=T ;cas++)
{
int n,m,c;
scanf("%d%d%d",&n,&m,&c);
for(int i=1;i<=3*n;i++) E[i].clear();
for(int i=1;i<=n;i++)
{
int x; scanf("%d",&x);
add(n+2*x-1,i,0);
add(i,n+2*x,0);
}
for(int i=1;i<n;i++)
{
add( n+2*(i+1),n+2*i-1,c);
add( n+2*i,n+2*(i+1)-1,c);
}
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
printf("Case #%d: %d\n",cas,dijstra(n));
}
return 0;
}