The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2554 Accepted Submission(s): 624
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
设虚点建图,然后优化下dijkstra就可以了
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
const int maxn=1e5+100;
struct Edge{
int to,w;
int next;
}e[maxn*6];
struct HeapNode
{
int w,u;
bool operator < (const HeapNode &a) const
{
return w>a.w;
}
};
int head[maxn*3],dis[maxn*3],edge;
int vis[maxn*3];
int n,m,c;
vector<int> pos[maxn];
void init()
{
MST(head,-1);
edge=0;
REP(i,n+1)
pos[i].clear();
}
void addedge(int u,int v,int w)
{
e[edge].to=v,e[edge].w=w,e[edge].next=head[u],head[u]=edge++;
}
void dijkstra(int s)
{
priority_queue<HeapNode> q;
CLR(vis);
for(int i=0;i<=3*n;i++) dis[i]=INF;
dis[s]=0;
HeapNode hp;
hp.u=1,hp.w=0;
q.push(hp);
while(!q.empty())
{
hp=q.top();q.pop();
int u=hp.u;
if(vis[u]) continue;
vis[u]=true;
for(int k=head[u];k!=-1;k=e[k].next)
{
int v=e[k].to;
if(dis[v]>dis[u]+e[k].w)
{
dis[v]=dis[u]+e[k].w;
hp.w=dis[v];
hp.u=v;
q.push(hp);
}
}
}
}
int check(int x)
{
if(x==INF)
return -1;
return x;
}
int main()
{
int T;
int cas=1;
cin>>T;
while(T--)
{
scanf("%d%d%d",&n,&m,&c);
init();
int x;
int max_layer=-1;
FOR(i,1,n)
{
scanf("%d",&x);
max_layer=max(max_layer,x);
pos[x].PB(i);
}
REP(i,max_layer)
{
REP(j,pos[i].size())
{
addedge(pos[i][j],i+n,c);
addedge(i+n+n,pos[i][j],0);
}
REP(j,pos[i+1].size())
{
addedge(i+n,pos[i+1][j],0);
addedge(pos[i+1][j],i+n+n,c);
}
}
int u,v,w;
REP(i,m)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
dijkstra(1);
printf("Case #%d: %d\n",cas++,check(dis[n]));
}
return 0;
}
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
const int maxn=1e5+100;
struct node{
int to,w;
node(){}
node(int v,int c){to=v;w=c;}
bool operator < (const node & a) const
{
if(w==a.w) return to<a.to;
else return w>a.w;
}
};
vector<node> e[maxn*6];
int dis[maxn*3];
int n,m,c;
vector<int> p[maxn*3];
void dijkstra(int s)
{
int i;
for(i=0;i<=3*n;i++) dis[i]=INF;
dis[s]=0;
priority_queue<node> q;
q.push(node(s,dis[s]));
while(!q.empty())
{
node u=q.top();
q.pop();
int v=u.to;
REP(i,e[v].size())
{
node temp=e[v][i];
if(dis[temp.to]>dis[v]+temp.w)
{
dis[temp.to]=dis[v]+temp.w;
q.push(node(temp.to,dis[temp.to]));
}
}
}
}
int check(int x)
{
if(x==INF)
return -1;
return x;
}
int main()
{
int T;
int cas=1;
cin>>T;
while(T--)
{
scanf("%d%d%d",&n,&m,&c);
REP(i,n*3+10)
{
p[i].clear();
e[i].clear();
}
int x;
int max_layer=-1;
FOR(i,1,n)
{
scanf("%d",&x);
max_layer=max(x,max_layer);
p[x].PB(i);
}
REP(i,max_layer)
{
REP(j,p[i].size())
{
e[p[i][j]].PB(node(i+n,c));
e[i+n+n].PB(node(p[i][j],0));
}
REP(j,p[i+1].size())
{
e[i+n].PB(node(p[i+1][j],0));
e[p[i+1][j]].PB(node(i+n+n,c));
}
}
int u,v,w;
REP(i,m)
{
scanf("%d%d%d",&u,&v,&w);
e[u].PB(node(v,w));
e[v].PB(node(u,w));
}
dijkstra(1);
printf("Case #%d: %d\n",cas++,check(dis[n]));
}
return 0;
}
本文介绍了一种解决特定图(Nya图)上的最短路径问题的方法。Nya图是一种具有分层结构的无向图,每层节点间通过固定成本相连,并额外包含一些边。文章提供了一个通过优化Dijkstra算法来求解从起始节点到终止节点最短路径的实例。
852

被折叠的 条评论
为什么被折叠?



