The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6215 Accepted Submission(s): 1390
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 <= 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.
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.
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
题意:
有n个点,每个点都在一个集合中。一个集合中的点可以到达任何一个相邻的集合中的任意一点。集合中的点不能互相到达。
给出n,m,c分别代表点的个数,边的个数,以及一个集合到相邻集合的权值c。
求一条1到n的最短路。
嗯,我们可以把每个集合看成一个点。嗯,首先因为集合中的点不能互相到达,所以我们应该把集合拆点,拆成一个入的和一个出的。
对于一个点p在一个集合A中,我们把A点拆成入点A1和出点A2。
那么建边p->A1以及A2->p。然后集合与集合中连边的时候。一个入点连接一个出点即可。
最后跑最短路。
这里要用双向队列来优化。就是小的点先更新,大的点后更新。
#include <bits/stdc++.h>
using namespace std;
int n,m,c,k;
const int MAXN=3e5+7;
const int inf=1e9;
vector<pair<int,int>>head[MAXN];
int dis[MAXN],vis[MAXN];
void spfa(int n)
{
for(int i=1; i<=n; ++i)
{
dis[i]=inf;
vis[i]=0;
}
deque<int>q;
q.push_front(1);
dis[1]=0;
while(!q.empty())
{
int u=q.front();
q.pop_front();
vis[u]=0;
for(int i=0,l=head[u].size(); i<l; ++i)
{
int v=head[u][i].first;
int w=head[u][i].second;
if(dis[u]+w<dis[v])
{
dis[v]=dis[u]+w;
if(!vis[v])
{
vis[v]=1;
if(q.empty()||v<q.front())q.push_front(v);
else q.push_back(v);
}
}
}
}
if(dis[n/3]>=inf)puts("-1");
else printf("%d\n",dis[n/3]);
}
int main()
{
int t;
scanf("%d",&t);
for(int tt=1; tt<=t; ++tt)
{
scanf("%d%d%d",&n,&m,&c);
for(int i=1,l=n*3; i<=l; ++i)
{
head[i].clear();
}
int x;
for(int i=1; i<=n; ++i)
{
scanf("%d",&x);
head[i].push_back(make_pair(x*2-1+n,0));
head[x*2+n].push_back(make_pair(i,0));
}
for(int i=1; i<=n; ++i)
{
head[i*2-1+n].push_back(make_pair(i*2+2+n,c));
head[i*2+1+n].push_back(make_pair(i*2+n,c));
}
int u,v,w;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
head[u].push_back(make_pair(v,w));
head[v].push_back(make_pair(u,w));
}
printf("Case #%d: ",tt);
spfa(n*3);
}
return 0;
}