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):

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII; const int MAXN = ;
const int MAXE = MAXN * ; int head[MAXN];
int to[MAXE], next[MAXE], cost[MAXE];
int n, m, ecnt, lcnt, c; void init() {
memset(head, , sizeof(head));
ecnt = lcnt = ;
} inline void add_edge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
} int dis[MAXN];
int lay[MAXN];
bool vis[MAXN]; void Dijkstra(int st, int ed) {
memset(dis, 0x7f, sizeof(dis));
memset(vis, , sizeof(vis));
priority_queue<PII> que; que.push(make_pair(, st));
dis[st] = ;
while(!que.empty()) {
int u = que.top().second; que.pop();
if(vis[u]) continue;
if(u == ed) return ;
vis[u] = true;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(dis[v] > dis[u] + cost[p]) {
dis[v] = dis[u] + cost[p];
que.push(make_pair(-dis[v], v));
}
}
}
} int T; inline int readint() {
char c = getchar();
while(!isdigit(c)) c = getchar();
int ret = ;
while(isdigit(c)) ret = ret * + c - '', c = getchar();
return ret;
} int main() {
T = readint();
for(int t = ; t <= T; ++t) {
n = readint(), m = readint(), c = readint();
init();
for(int i = ; i <= n; ++i) {
lay[i] = readint();
add_edge(i, n + * lay[i] - , );
add_edge(n + * lay[i], i, );
}
for(int i = ; i < n; ++i) {
add_edge(n + * i - , n + * (i + ), c);
add_edge(n + * (i + ) - , n + * i, c);
}
int u, v, w;
while(m--) {
//scanf("%d%d%d", &u, &v, &w);
u = readint(), v = readint(), w = readint();
add_edge(u, v, w);
add_edge(v, u, w);
}
Dijkstra(, n);
if(dis[n] == 0x7f7f7f7f) dis[n] = -;
printf("Case #%d: %d\n", t, dis[n]);
}
}

HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)的更多相关文章

  1. HDU 4717 The Moving Points(三分法)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description There are N points in total. Every point moves in certain direction and certain speed. W ...

  2. HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description If we sum up every digit of a number and the result can be exactly divided by 10, we say ...

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

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

  4. 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 ...

  5. 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 ...

  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 (最短路)

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

  8. 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 ...

  9. (中等) 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 ...

随机推荐

  1. BionicApi 学习笔记

    1.内存管理 malloc, realloc, free new, delete2.文件输入操作 fopen, fwrite, fputs, fputc, fprintf, fflush fread, ...

  2. C++ push方法与push_back方法

    [转载他人的文章,下午在搞c++,谢谢共享] [摘要] push与push_back是STL中常见的方法,都是向数据结构中添加元素.初识STL,对于添加元素的方法以产生混淆,这里暂对两种方法作出比较分 ...

  3. 在C++中如何实现文件的读写

    一.ASCII 输出为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstream>取代< fstream.h>,所 ...

  4. PG进程结构和内存结构

    ​ 本文主要介绍PostgreSQL数据库(后文简称PG)进程结构和内存结构,物理结构将在后续继续整理分享. ​ 上图描述了PG进程结构.内存结构和部分物理结构的内容.图中的内容包含了两个部分: PG ...

  5. mysql-新增表前判断同名表是否存在

    新增多个表时,如果有同名表会报错,导致其中一个表不能正确创建,此时可以用以下语句进行判断: DROP TABLE IF EXISTS USER; --判断表是否存在,如果存在就删除! CREATE T ...

  6. jdbc学习笔记02

    数据库连接池 DBCP DataBase Conection Pool:数据库连接池 如果没有数据库连接池,每一次业务都需要服务器和数据库服务器建立一次连接,业务处理完连接断开,如果有1万次业务处理, ...

  7. Linux常用文档操作命令--1

    1.查看目录下的文档 a) ls(list):查看目录下的所有文档或者文档的信息. 命令行:ls [-a][-A] [-f][-F][-h][-l][-r][-R][-S][-t] 目录名称  //注 ...

  8. HTML中的【块】与【内嵌】

    块元素与内嵌元素 块的特征 默认独占一行 没有宽度时默认撑满一行 支持所有的css命令 内嵌的特征 同行可以连续跟同类的标签 内容撑开宽度 不支持宽高 不支持上下的内外边距 代码换行被解析 块与内嵌的 ...

  9. 【shell脚本学习-4】

    文本处理 #!/bin/bash#----------文本处理---------- #---------------echo----------------- # "-n":处理光 ...

  10. Hadoop Eclipse 插件制作以及安装

    在本地使用Eclipse调试MapReduce程序,需要Hadoop插件,笔摘记录下制作安装过程. 准备工作(hadoop-2.6.0为例): 搭建好Hadoop环境 下载Hadoop安装包,解压到某 ...