主要是建图,建好图之后跑一边dijkstra即可。

一共3N个点,1~N是原图中的点1~N,然后把每层x拆成两个点(N+x)[用于连指向x层的边]和(N+N+x)[用于连从x层指出的边]。

相邻层节点互相可达:AddEdge( N+N+x+1, N+x, C), AddEdge( N+N+x, N+x+1, C);

对于位于x层的节点i,AddEdge(N+x, i, 0), AddEdge(i, N+N+x, 0);

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue> using namespace std; const int MAXN = *;
const int INF = << ; struct HeapNode
{
int d, u;
HeapNode() { }
HeapNode( int _d, int _u ): d(_d), u(_u) { }
bool operator<( const HeapNode& rhs ) const
{
return d > rhs.d;
}
}; struct Edge
{
int from, to, dist;
Edge() { }
Edge( int f, int t, int d ) : from(f), to(t), dist(d) { }
}; struct Dijkstra
{
int n, m;
vector<Edge> edges;
vector<int> G[MAXN];
bool done[MAXN];
int d[MAXN], p[MAXN]; void init( int n )
{
this->n = n;
for ( int i = ; i <= n; ++i ) G[i].clear();
edges.clear();
return;
} void AddEdge( int from, int to, int dist )
{
edges.push_back( Edge( from, to, dist ) );
m = edges.size();
G[from].push_back(m - );
return;
} void dijkstra( int s )
{
priority_queue<HeapNode> Q;
for ( int i = ; i <= n; ++i ) d[i] = INF;
d[s] = ;
memset( done, , sizeof(done) );
Q.push( HeapNode( , s ) );
while ( !Q.empty() )
{
HeapNode x = Q.top();
Q.pop();
int u = x.u;
if ( done[u] ) continue;
done[u] = true;
for ( int i = ; i < (int)G[u].size(); ++i )
{
Edge& e = edges[ G[u][i] ];
if ( d[e.to] > d[u] + e.dist )
{
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
Q.push( HeapNode( d[e.to], e.to ) );
}
}
}
return;
}
}; int N, M, C;
Dijkstra slv;
bool vis[MAXN/]; int main()
{
int T, cas = ;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%d%d%d", &N, &M, &C ); memset( vis, false, sizeof(bool)*(N+) );
slv.init( N* ); for ( int i = ; i <= N; ++i )
{
int layer;
scanf( "%d", &layer );
slv.AddEdge( N + layer, i, );
slv.AddEdge( i, N + N + layer, );
vis[layer] = true;
} for ( int i = ; i < N; ++i )
{
if ( vis[i] && vis[i + ] )
{
slv.AddEdge( N + N + i, N + i + , C );
slv.AddEdge( N + N + i + , N + i, C );
}
} for ( int i = ; i < M; ++i )
{
int u, v, w;
scanf( "%d%d%d", &u, &v, &w );
slv.AddEdge( u, v, w );
slv.AddEdge( v, u, w );
} slv.dijkstra( ); printf( "Case #%d: ", ++cas );
if ( slv.d[N] < INF ) printf( "%d\n", slv.d[N] );
else puts("-1");
}
return ;
}

HDU 4725 The Shortest Path in Nya Graph( 建图 + 最短路 )的更多相关文章

  1. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  2. HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]

    HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...

  3. HDU 4725 The Shortest Path in Nya Graph

    he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...

  4. HDU 4725 The Shortest Path in Nya Graph(构图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. HDU 4725 The Shortest Path in Nya Graph (最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. hdu 4725 The Shortest Path in Nya Graph (最短路+建图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  8. HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  9. HDU 4725 The Shortest Path in Nya Graph (最短路 )

    This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...

  10. HDU - 4725 The Shortest Path in Nya Graph 【拆点 + dijkstra】

    This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...

随机推荐

  1. 相机标定/校正(Camera Calibration)

    以前DIP課程有做過Camera calibration,這次因為用Gopro做Visual SLAM,所以又要撿一下校正的過程.主要還是張正友的方法. OpenCV: 用OpenCV自帶的Sampl ...

  2. 第20章 USART—串口通讯—零死角玩转STM32-F429系列

    第20章      USART—串口通讯 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fi ...

  3. JS isArray、typeof、instanceof

    Array.isArray() 用来检验是不是数组 var a = [1,2,3] console.log(typeof a); // object console.log(Array.isArray ...

  4. Colours–颜色库,包含100种预定义的颜色和方法

    简介 Colours–颜色库,包含各种100种预定义的颜色和方法,可以简化颜色相关的开发工作. 项目主页: Colours 最新示例: 点击下载 快速入门 安装 通过Cocoapods安装 pod ' ...

  5. 51nod 1298 圆与三角形——计算几何

    题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1298 转化成判断三条线段和圆是否

  6. tp3.2 excel导出

    //导出操作 function exportExcel($expTitle,$expCellName,$expTableData,$names,$width){ $xlsTitle = iconv(' ...

  7. jstree 全部选中事件 select_all 使用

    select_all function of jstree not checked node for jstree-open branch of ajax-jstree 很尴尬啊,找了整个百度,360 ...

  8. 正则(re 模块)

    就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹 ...

  9. VS2013未能正确加载的问题【转载】

    今天使用电脑,关机重启时,WINDOWS提示“正在配置中,请勿关机” 的提醒,等重启后,打开VS2013就提示了未加载成功的问题,如下图: 我的解决方法是:找到VS2013开发人员命令提示:在窗口中输 ...

  10. tcl之其他命令-eval/source