HDU-4725 The Shortest Path in Nya Graph(最短路)
2017-03-30 10:32
369 查看
HDU-4725
n个点,m条无向边,且每个点属于一个层,层间的点相互到达花费为固定的C,求1到n最短路
明显如果层间所有点连边复杂度会爆炸,建图把每层拆成两个点,分成点a和点b
该层的点到a点花费是c,a点到下一层的所有点的花费是0,下一层的所有点到点b的花费是c,点b到该层的所有点的花费是0
这样可以保证该层点可通过层间关系到达该层任意点,且花费是2c
这种建图卡栈和队列SPFA(当然也可能我写法有问题)
下为从网上找到的另一种建图法,SPFA可过
将层抽象出来成为n个点(对应编号依次为n+1~n+n),然后层与层建边,点与点建边,层与在该层上的点建边 (边长为0),点与相邻层建边 (边长为c)。
ps:这样处理就 不用拆点 了。不过要注意的是 相邻两层必须都要有点才建边 (不然会WA,可以参考我贴的数据)。
建图方法1代码
n个点,m条无向边,且每个点属于一个层,层间的点相互到达花费为固定的C,求1到n最短路
明显如果层间所有点连边复杂度会爆炸,建图把每层拆成两个点,分成点a和点b
该层的点到a点花费是c,a点到下一层的所有点的花费是0,下一层的所有点到点b的花费是c,点b到该层的所有点的花费是0
这样可以保证该层点可通过层间关系到达该层任意点,且花费是2c
这种建图卡栈和队列SPFA(当然也可能我写法有问题)
下为从网上找到的另一种建图法,SPFA可过
将层抽象出来成为n个点(对应编号依次为n+1~n+n),然后层与层建边,点与点建边,层与在该层上的点建边 (边长为0),点与相邻层建边 (边长为c)。
ps:这样处理就 不用拆点 了。不过要注意的是 相邻两层必须都要有点才建边 (不然会WA,可以参考我贴的数据)。
建图方法1代码
/*卡栈和队列SPFA(= =) dij+堆过 明显如果层间所有点连边复杂度会爆炸,建图把每层拆成两个点,分成点a和点b 该层的点到a点花费是c,a点到下一层的所有点的花费是0,下一层的所有点到点b的花费是c,点b到该层的所有点的花费是0 这样可以保证该层点可通过层间关系到达该层任意点,且花费是2c */ #include <cstdio> #include <algorithm> #include <queue> #include <cstring> using namespace std; typedef pair<int,int> PII; const int MAXN=3e5+7; const int MAXM=MAXN; const int INF=0x3f3f3f3f; int T,head[MAXN],to[MAXM<<1],ne[MAXM<<1],wt[MAXM<<1],n,m,dist[MAXN],ecnt; void init() { ecnt=0; memset(head,0,sizeof(head)); } void addedge(int a,int b,int c) { ne[++ecnt]=head[a]; head[a]=ecnt; to[ecnt]=b; wt[ecnt]=c; } int vis[MAXN]; priority_queue<PII> pq; int dijkstra(int s,int t)//s为源点,t为终点,路径权值非负 { for(int i=1;i<=n;i++) dist[i]=INF,vis[i]=0; dist[s]=0; pq.push(PII(0,s)); while(!pq.empty()) { int milb=pq.top().second; pq.pop(); if(vis[milb]) continue; vis[milb]=1; for(int j=head[milb];j;j=ne[j]) { int v=to[j]; if(!vis[v]&&dist[v]>dist[milb]+wt[j]) { dist[v]=dist[milb]+wt[j]; pq.push(PII(-dist[v],v)); } } } if(dist[t]==INF) return -1; return dist[t]; } int main() { int ta,tb,tc; scanf("%d",&T); for(int kase=1;kase<=T;kase++) { init(); scanf("%d%d%d",&n,&m,&tc); for(int i=1;i<=n;i++) { scanf("%d",&ta); addedge(i,n+ta,tc); addedge(2*n+ta,i,0); if(--ta) { addedge(n+ta,i,0); addedge(i,2*n+ta,tc); } } for(int i=0;i<m;i++) { scanf("%d%d%d",&ta,&tb,&tc); addedge(ta,tb,tc); addedge(tb,ta,tc); } n*=3; printf("Case #%d: %d\n",kase,dijkstra(1,n/3)); } return 0; }
相关文章推荐
- HDU 4725 The Shortest Path in Nya Graph(图论+最短路)
- HDU 4725 The Shortest Path in Nya Graph (最短路)
- HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】
- HDU 4725-J - The Shortest Path in Nya Graph-增点建图-层次网络-最短路
- hdu 4725 The Shortest Path in Nya Graph 最短路 拆点
- HDU 4725 The Shortest Path in Nya Graph (最短路拆点建图)
- HDU 4725 The Shortest Path in Nya Graph 【最短路之建图难点】
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
- 【HDU】4725 The Shortest Path in Nya Graph 最短路
- HDU 4725 The Shortest Path in Nya Graph(最短路拆点)
- HDU 4725 The Shortest Path in Nya Graph(最短路)
- hdu 4725 The Shortest Path in Nya Graph(堆+dij,最短路,5级)
- HDU 4725 The Shortest Path in Nya Graph (最短路)
- HDU 4725 The Shortest Path in Nya Graph (最短路)
- HDU 4725 The Shortest Path in Nya Graph( 建图 + 最短路 )
- HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】
- The Shortest Path in Nya Graph HDU - 4725(最短路,spfa)
- HDU 4725 The Shortest Path in Nya Graph(最短路)
- hdu 4725 The Shortest Path in Nya Graph(堆+dij,最短路,5级)
- HDU-4725 The Shortest Path in Nya Graph( 最短路 )