hdu 4725

The Shortest Path in Nya Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2296    Accepted Submission(s): 561

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
 
Source
 
Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4822 4821 4820 4819 4818 
 
最短路
把每一层拆成2 个点 一个是连接结点入边,一个是连接结点出边,每层连接结点入边的点连接下个层连接结点出边的点,下一层连接入边的点连接上一层连接出边的点。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; const int MAX_N = 3e5 + ;
const int INF = 1e9 + ;
typedef long long ll;
struct heapnode {
int d;
int u;
bool operator < (const heapnode &rhs) const {
return d > rhs.d;
}
};
struct Edge {int from, to, cost;}; vector<int> G[MAX_N];
vector<Edge> edges;
int N,M,C;
int d[MAX_N];
bool done[MAX_N];
vector <int> lay[MAX_N]; void add_edge(int from, int to, int cost) {
edges.push_back((Edge){from, to, cost});
int m = edges.size();
G[from].push_back(m - );
} void dij(int s) {
memset(done,,sizeof(done));
for(int i = ; i <= * N; ++i) {
d[i] = INF;
}
d[s] = ;
priority_queue <heapnode> q;
q.push((heapnode) {d[s], s}); while(!q.empty()) {
heapnode x = q.top(); q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = ;
if(u == N) return; for(int i = ; i < G[u].size(); ++i) {
Edge &e = edges[ G[u][i] ];
if(d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
q.push((heapnode) {d[e.to], e.to});
}
}
}
}
int main()
{ // freopen("sw.in","r",stdin);
int t;
scanf("%d",&t);
int ca = ;
while(t--) {
scanf("%d%d%d",&N,&M,&C);
for(int i = ; i <= * N; ++i) G[i].clear();
edges.clear(); for(int i = ; i <= N; ++i) {
int ch;
scanf("%d",&ch);
add_edge(i, ch + N, );
add_edge(ch + * N, i, ); } /*for(int i = 2 * N + 1; i <= 3 * N; ++i) {
add_edge(i, i - N, 0);
}*/ for(int i = N + ; i <= * N - ; ++i) {
add_edge(i, i + + N, C);
add_edge(i + , i + N, C);
} for(int i = ; i <= M; ++i) {
int u, v, w;
scanf("%d%d%d",&u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w);
} dij();
printf("Case #%d: %d\n",ca++, (d[N] == INF) ? - : d[N]);
} return ;
}
上一篇:Java RandomAccessFile用法(转载)


下一篇:partial与sorted