【HDU4725】【建模dij】【新建虚拟节点 每层一个出口和入口】
来源:互联网 发布:大数据专业 编辑:程序博客网 时间:2024/05/19 00:48
The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3874 Accepted Submission(s): 911
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
23 3 31 3 21 2 12 3 11 3 33 3 31 3 21 2 22 3 21 3 4
Sample Output
Case #1: 2Case #2: 3
Source
2013 ACM/ICPC Asia Regional Online —— Warmup2
Recommend
zhuyuanchen520
#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define mp push_backint T;int n,m,c;int H[100010*3];struct Edge{int v,c,next;}edge[100010*6+100010];int top;int d[100010*3];bool ok[100010*3];void addedge(int u,int v,int c){edge[top] = {v,c,H[u]};H[u] = top++;}struct Qnode{int idx,dis;Qnode(){};Qnode(int _i,int _d):idx(_i),dis(_d){};bool operator<(const Qnode&x) const{return dis > x.dis;}};// +n 是入口 +2n是出口int dij(int st){for(int i=1;i<=3*n;i++){ok[i] = false;d[i] = 0x3f3f3f3f;}d[st] = 0;priority_queue<Qnode> pq;pq.push(Qnode(st,0));while(!pq.empty()){Qnode cur = pq.top();pq.pop();if(ok[cur.idx]) continue;ok[cur.idx] = true;for(int ei=H[cur.idx];ei!=-1;ei=edge[ei].next){Edge e = edge[ei];if(!ok[e.v] && d[e.v] > d[cur.idx] + e.c){d[e.v] = d[cur.idx] + e.c;pq.push(Qnode(e.v,d[e.v]));}}}return d[n] == 0x3f3f3f3f ? -1:d[n];}int main(){ scanf("%d",&T);int C = 1;while(T--){scanf("%d%d%d",&n,&m,&c);top = 0;memset(H,-1,sizeof(H));for(int i=1;i<=n;i++){int l;scanf("%d",&l);addedge(n+l,i,0);addedge(i,2*n+l,0);}for(int i=2;i<=n;i++){addedge(i-1+2*n,i+n,c);addedge(i+2*n,i-1+n,c);}for(int i=1;i<=m;i++){int u,v,c;scanf("%d%d%d",&u,&v,&c);addedge(u,v,c);addedge(v,u,c);}printf("Case #%d: %d\n",C++,dij(1));} return 0;}
0 0
- 【HDU4725】【建模dij】【新建虚拟节点 每层一个出口和入口】
- 程序的入口和出口
- 第三节、配置文件:入口和出口
- 一个求迷宫入口到出口最近距离的程序
- 关于求树中每层的节点总数
- 输出二叉树每层节点数量
- HDU4725
- 统计二叉树每层节点个数并打印每层节点
- caffe: 新建一个loss层
- 判断一个有环链表的入口节点
- 统计二叉树每层节点的个数
- 分层访问二叉树的节点,每层换行
- Leetcode637. BFS计算二叉树每层节点的平均值
- 一个求迷宫入口到出口最近距离的程序 JAVA版本
- LCC编译器的源程序分析(45)函数代码入口和出口的代码生成
- [转载]LCC编译器的源程序分析(45)函数代码入口和出口的代码生成
- (莱昂氏unix源代码分析导读-20)中断、陷入的入口和出口
- GNU 的入口和出口配置函数的使用以及函数调用栈追踪
- Unable to open debugger port : java.net.BindException "Address already in use: JVM_Bind
- ListView,GridView和ScrollView设置初始间距不影响滚动。
- 通过OpenSSL获取证书扩展属性之一:“基本约束”
- 将Gradle项目发布到maven仓库
- 模糊背景处理--当前界面背景
- 【HDU4725】【建模dij】【新建虚拟节点 每层一个出口和入口】
- 8.2.1.7 Use of Index Extensions 索引扩展使用
- Extjs grid select 选取行内容
- UVA_10271_Chopsticks
- Linux常用指令
- IOS集成支付宝回调的坑
- 利用栈判断链表是否为回文
- 利用runtime检测这个对象是否存在某属性?
- Android 中的 EditText