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): 1319 Accepted Submission(s): 293
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.
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.
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.
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
构造虚拟节点,然后见图,dijstra优先队列求最短路。
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
#include<list>
#include<set>
#include<queue>
#include<stack>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=1000010;
struct qnode
{
int v;
int c;
qnode(int _v=0,int _c=0):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
int v,cost;
Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
void fun(int n,int start)
{
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=0;
que.push(qnode(start,0));
qnode tmp;
while(!que.empty())
{
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=0;i<E[u].size();i++)
{
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
void add(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
}
int main()
{
int i,j,k,m,t,T,C,p,q,g,h,u,v,w,n;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
scanf("%d%d%d",&n,&m,&C);
for( i = 1;i <= 3*n;i++) E[i].clear();
for( i = 1;i <= n;i++)
{
scanf("%d",&u);
add(i,n + 2*u - 1,0);
add(n + 2*u ,i,0);
}
for( i = 1;i < n;i++)
{
add(n + 2*i-1,n + 2*(i+1),C);
add(n + 2*(i+1)-1,n + 2*i,C);
}
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
printf("Case #%d: ",t);
for(i=1;i<=3*n;i++)dist[i]=INF;
fun(3*n,1);
int ans;
if(dist[n]>=INF)ans=-1;
else ans=dist[n];
printf("%d\n",ans);
}
return 0;
}
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
#include<list>
#include<set>
#include<queue>
#include<stack>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=1000010;
struct qnode
{
int v;
int c;
qnode(int _v=0,int _c=0):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
int v,cost;
Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
void fun(int n,int start)
{
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=0;
que.push(qnode(start,0));
qnode tmp;
while(!que.empty())
{
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=0;i<E[u].size();i++)
{
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
void add(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
}
int main()
{
int i,j,k,m,t,T,C,p,q,g,h,u,v,w,n;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
scanf("%d%d%d",&n,&m,&C);
for( i = 1;i <= 3*n;i++) E[i].clear();
for( i = 1;i <= n;i++)
{
scanf("%d",&u);
add(i,n + 2*u - 1,0);
add(n + 2*u ,i,0);
}
for( i = 1;i < n;i++)
{
add(n + 2*i-1,n + 2*(i+1),C);
add(n + 2*(i+1)-1,n + 2*i,C);
}
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
printf("Case #%d: ",t);
for(i=1;i<=3*n;i++)dist[i]=INF;
fun(3*n,1);
int ans;
if(dist[n]>=INF)ans=-1;
else ans=dist[n];
printf("%d\n",ans);
}
return 0;
}

浙公网安备 33010602011771号