主要是建图,建好图之后跑一边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. 剑指offer 33 把数组排成最小的数

    错误代码 class Solution { public: int FindGreatestSumOfSubArray(vector<int> array) { int length = ...

  2. linux中CURL的安装

    curl是一款著名的字符界面下的下载工具,支持HTTP.HTTPS.FTP.FTPS.DICT.TELNET.LDAP.FILE,和GOPHER.此外还具有cookies支持.断点续传.FTP上传.密 ...

  3. xshell 连接虚拟机过程

    (1)Ctrl+Shift+T 打开终端 terminal (2)ifconfig得到ip网络地址 (3)ssh安装已经打开ssh服务 (4)安装openssh-server sudo apt ins ...

  4. MFC项目依赖 BCG框架示例

    1.创建一个简单的MFC工程: 2.将BCG框架项目导入到新建的mfc解决方案中,例如将BCGCBPro\BCGCBPRO140.vcxproj添加到解决方案. 3.修改mfc项目属性,包含BCG框架 ...

  5. 牛客小白月赛2 G 文 【模拟】

    链接:https://www.nowcoder.com/acm/contest/86/G来源:牛客网 题目描述 Sεlιнα(Selina) 开始了新一轮的男友海选.她要求她的男友要德智体美劳样样都全 ...

  6. tomcat日志切割脚本

    tomcat日志每俩小时切割的脚本如下(这是用定时任务来完成的,此方法无需重启tomcat): time=$(date +%H) end_time=`` a=$end_time BF_TIME=$(- ...

  7. python join() 提示UnicodeDecodeError: 'utf8' codec can't decode byte 0xcb in position 0: unexpected end of的原因及解决办法

    问题: 在使用join()将列表元素连接成字符串时出错如下 return split.join(result) UnicodeDecodeError: 'utf8' codec can't decod ...

  8. Java OOP——第五章 异常

    1. 尝试通过if-else来解决异常问题: Eg: public class Test2 {       public static void main(String[] args) {       ...

  9. TP5 行为Behavior用法说明

    TP5 行为Behavior用法说明 无论是tp3还是在tp5中,行为都是一个非常重要的概念,关于太多的理论知识,就不多说了,不了解的请查看开发文档:TP5 行为概述 以下,就由代码来一步一步实现行为 ...

  10. 裸机——RTC

    1. 首先晓得RTC的基本知识 RTC被划分到timer,但RTC是面向时间点的. 如果按照定时器的思路去思考,那么应该考虑 时间周期 和 计数值. RTC 不是面向时间点的,所以略有不同, 时间周期 ...