热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HDU4725TheShortestPathinNyaGraph(最短路径)(2013ACM/ICPCAsiaRegionalOnline――Warmup2)

DescriptionThisisaveryeasyproblem,yourtaskisjustcalculateelcaminomascortoe

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 <= 10   5) and C(1 <= C <= 10   3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.   The second line has N numbers l   i  (1 <= l   i  <= N), which is the layer of i   th  node belong to.   Then come N lines each with 3 numbers, u, v (1 <= u, v <=N, u <> v) and w (1 <= w <= 10   4), 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.

 

题目大意:有n个点m条无向边,然后每个点有一个层次,相邻层次的点可以移动(同层次不可以),花费为C,问1~n的最小花费。

思路:我试过类似于每层之间的点都连一条边(不是$O(n^2)$那种很挫的方法,不断TLE……好吧换思路……每层新建两个点a、b,i层的点到ai连一条费用为0的边,bi到i层的点连一条边,然后相邻的层分别从ai到bj连一条边,费用为C。跑Dijkstra+heap可AC。

PS:至于最初的思路为啥会TLE我就不想说了……有兴趣自己动脑吧……

 

代码(343MS):

 1 #include 
2 #include
3 #include
4 #include
5 #include
6 using namespace std;
7 typedef pair<int, int> PII;
8
9 const int MAXN = 300010;
10 const int MAXE = MAXN * 2;
11
12 int head[MAXN];
13 int to[MAXE], next[MAXE], cost[MAXE];
14 int n, m, ecnt, lcnt, c;
15
16 void init() {
17 memset(head, 0, sizeof(head));
18 ecnt = lcnt = 1;
19 }
20
21 inline void add_edge(int u, int v, int c) {
22 to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
23 }
24
25 int dis[MAXN];
26 int lay[MAXN];
27 bool vis[MAXN];
28
29 void Dijkstra(int st, int ed) {
30 memset(dis, 0x7f, sizeof(dis));
31 memset(vis, 0, sizeof(vis));
32 priority_queue que; que.push(make_pair(0, st));
33 dis[st] = 0;
34 while(!que.empty()) {
35 int u = que.top().second; que.pop();
36 if(vis[u]) continue;
37 if(u == ed) return ;
38 vis[u] = true;
39 for(int p = head[u]; p; p = next[p]) {
40 int &v = to[p];
41 if(dis[v] > dis[u] + cost[p]) {
42 dis[v] = dis[u] + cost[p];
43 que.push(make_pair(-dis[v], v));
44 }
45 }
46 }
47 }
48
49 int T;
50
51 inline int readint() {
52 char c = getchar();
53 while(!isdigit(c)) c = getchar();
54 int ret = 0;
55 while(isdigit(c)) ret = ret * 10 + c - '0', c = getchar();
56 return ret;
57 }
58
59 int main() {
60 T = readint();
61 for(int t = 1; t <= T; ++t) {
62 n = readint(), m = readint(), c = readint();
63 init();
64 for(int i = 1; i <= n; ++i) {
65 lay[i] = readint();
66 add_edge(i, n + 2 * lay[i] - 1, 0);
67 add_edge(n + 2 * lay[i], i, 0);
68 }
69 for(int i = 1; i i) {
70 add_edge(n + 2 * i - 1, n + 2 * (i + 1), c);
71 add_edge(n + 2 * (i + 1) - 1, n + 2 * i, c);
72 }
73 int u, v, w;
74 while(m--) {
75 //scanf("%d%d%d", &u, &v, &w);
76 u = readint(), v = readint(), w = readint();
77 add_edge(u, v, w);
78 add_edge(v, u, w);
79 }
80 Dijkstra(1, n);
81 if(dis[n] == 0x7f7f7f7f) dis[n] = -1;
82 printf("Case #%d: %d\n", t, dis[n]);
83 }
84 }
View Code

 


推荐阅读
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • STL迭代器的种类及其功能介绍
    本文介绍了标准模板库(STL)定义的五种迭代器的种类和功能。通过图表展示了这几种迭代器之间的关系,并详细描述了各个迭代器的功能和使用方法。其中,输入迭代器用于从容器中读取元素,输出迭代器用于向容器中写入元素,正向迭代器是输入迭代器和输出迭代器的组合。本文的目的是帮助读者更好地理解STL迭代器的使用方法和特点。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 李逍遥寻找仙药的迷阵之旅
    本文讲述了少年李逍遥为了救治婶婶的病情,前往仙灵岛寻找仙药的故事。他需要穿越一个由M×N个方格组成的迷阵,有些方格内有怪物,有些方格是安全的。李逍遥需要避开有怪物的方格,并经过最少的方格,找到仙药。在寻找的过程中,他还会遇到神秘人物。本文提供了一个迷阵样例及李逍遥找到仙药的路线。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 本文介绍了H5游戏性能优化和调试技巧,包括从问题表象出发进行优化、排除外部问题导致的卡顿、帧率设定、减少drawcall的方法、UI优化和图集渲染等八个理念。对于游戏程序员来说,解决游戏性能问题是一个关键的任务,本文提供了一些有用的参考价值。摘要长度为183字。 ... [详细]
  • linux进阶50——无锁CAS
    1.概念比较并交换(compareandswap,CAS),是原⼦操作的⼀种,可⽤于在多线程编程中实现不被打断的数据交换操作࿰ ... [详细]
author-avatar
csc1520075
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有