hdu4725——The Shortest Path in Nya Graph(SPFA+两层图)
来源:互联网 发布:python调用caffe测试 编辑:程序博客网 时间:2024/06/06 12:26
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
挺有意思的最短路
题意是在无向图中,每个节点还属于一个层(layer),可以从第i个层的节点移动到第i+1个层的节点,花费是c。然后是给出一个普通的无向图,求1到n的最短路。
因为有层的存在,在寻找最短路的时候可能直接经过层,不妨把层也当做节点,层与所属节点之间的花费是0,表示随时可以从层转移到节点上寻找最短路,然后层与层之间的花费是c,节点到下一层或上一层的的花费也是c,节点与节点之间的花费是w,方便起见,层的节点编号从n+1到n+n。
另外数组开大点,不然会TLE,我很奇怪为什么不是RE,害得我判断失误
#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 200010#define Mod 10001using namespace std;struct Edge{ int v,w,next;};Edge edge[MAXN*20];int head[MAXN],n,m,e,vis[MAXN],dis[MAXN];void add(Edge *edge,int *head,int u,int v,int w){ edge[e].v=v; edge[e].w=w; edge[e].next=head[u]; head[u]=e; e++;}void spfa(Edge *edge,int *head,int u){ memset(vis,0,sizeof(vis)); for(int i=1; i<=2*n; ++i) //这里WA了 dis[i]=INF; dis[u]=0; queue<int> q; q.push(u); while(!q.empty()) { u=q.front(); q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v,w=edge[i].w; if(w+dis[u]<dis[v]) { dis[v]=w+dis[u]; if(!vis[v]) { vis[v]=1; q.push(v); } } } }}int layer[MAXN];int main(){ int t,c,u,v,w; scanf("%d",&t); for(int cnt=1; cnt<=t; ++cnt) { e=0; scanf("%d%d%d",&n,&m,&c); memset(vis,0,sizeof(vis)); memset(head,-1,sizeof(head)); for(int i=1; i<=n; ++i) { scanf("%d",&u); layer[i]=u; vis[u]=1; } for(int i=1; i<n; ++i) { if(vis[i]&&vis[i+1]) { add(edge,head,i+n,i+n+1,c); add(edge,head,i+n+1,i+n,c); } } for(int i=1;i<=n;++i) { add(edge,head,n+layer[i],i,0); //add(edge,head,i,n+layer[i],0); 有多个节点对应一个层的情况,大概。。。 if(layer[i]>1) add(edge,head,i,n+layer[i]-1,c); if(layer[i]<n) add(edge,head,i,n+layer[i]+1,c); } for(int i=1;i<=m;++i) { scanf("%d%d%d",&u,&v,&w); add(edge,head,u,v,w); add(edge,head,v,u,w); } spfa(edge,head,1); if(dis[n]<INF) printf("Case #%d: %d\n",cnt,dis[n]); else printf("Case #%d: -1\n",cnt); } return 0;}
- hdu4725——The Shortest Path in Nya Graph(SPFA+两层图)
- hdu4725—The Shortest Path in Nya Graph(spfa+建图)
- The Shortest Path in Nya Graph HDU4725 spfa||dijkstra
- hdu4725 The Shortest Path in Nya Graph
- hdu4725 The Shortest Path in Nya Graph
- HDU4725-The Shortest Path in Nya Graph
- 2013成都邀请赛J题||HDU4725 The Shortest Path in Nya Graph(spfa+slf优化最短路)
- hdu4725(The Shortest Path in Nya Graph)最短路
- HDU 4725 The Shortest Path in Nya Graph(SPFA)
- hdu The Shortest Path in Nya Graph(建图+spfa)
- HDU 4725 The Shortest Path in Nya Graph priority_queue + spfa
- HDU 4725 The Shortest Path in Nya Graph 建图加SPFA
- hdu 4725 The Shortest Path in Nya Graph //spfa
- HDU 4725 —— The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph(最短路 SPFA 建图)
- HDOJ 题目4725 The Shortest Path in Nya Graph(spfa,建图)
- hdu-4725 The Shortest Path in Nya Graph (spfa + 建图)
- The Shortest Path in Nya Graph
- [lintcode] Binary Tree Maximum Path Sum II
- POJ 2409 Let it Bead (Polya) .
- 随手整理
- css left/right拉伸和width同时存在
- ios-正则表达式
- hdu4725——The Shortest Path in Nya Graph(SPFA+两层图)
- POJ 1655 (树dp or 树重心)
- kali 2.0中metasploit的使用教程
- Universal-Image-Loader源码阅读(8)-core/imageaware/NonViewAware
- poj1328 贪心
- 2016.8.15 夏令营 Day15
- CALayer&Core Animation核心动画
- Picasso获取得到的Bitmap
- codeforces 510.B Fox And Two Dots (DFS好题)