HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description
Input
Output
题目大意:有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)的更多相关文章
- 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 ...
- 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 ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- (中等) 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 ...
随机推荐
- BionicApi 学习笔记
1.内存管理 malloc, realloc, free new, delete2.文件输入操作 fopen, fwrite, fputs, fputc, fprintf, fflush fread, ...
- C++ push方法与push_back方法
[转载他人的文章,下午在搞c++,谢谢共享] [摘要] push与push_back是STL中常见的方法,都是向数据结构中添加元素.初识STL,对于添加元素的方法以产生混淆,这里暂对两种方法作出比较分 ...
- 在C++中如何实现文件的读写
一.ASCII 输出为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstream>取代< fstream.h>,所 ...
- PG进程结构和内存结构
本文主要介绍PostgreSQL数据库(后文简称PG)进程结构和内存结构,物理结构将在后续继续整理分享. 上图描述了PG进程结构.内存结构和部分物理结构的内容.图中的内容包含了两个部分: PG ...
- mysql-新增表前判断同名表是否存在
新增多个表时,如果有同名表会报错,导致其中一个表不能正确创建,此时可以用以下语句进行判断: DROP TABLE IF EXISTS USER; --判断表是否存在,如果存在就删除! CREATE T ...
- jdbc学习笔记02
数据库连接池 DBCP DataBase Conection Pool:数据库连接池 如果没有数据库连接池,每一次业务都需要服务器和数据库服务器建立一次连接,业务处理完连接断开,如果有1万次业务处理, ...
- Linux常用文档操作命令--1
1.查看目录下的文档 a) ls(list):查看目录下的所有文档或者文档的信息. 命令行:ls [-a][-A] [-f][-F][-h][-l][-r][-R][-S][-t] 目录名称 //注 ...
- HTML中的【块】与【内嵌】
块元素与内嵌元素 块的特征 默认独占一行 没有宽度时默认撑满一行 支持所有的css命令 内嵌的特征 同行可以连续跟同类的标签 内容撑开宽度 不支持宽高 不支持上下的内外边距 代码换行被解析 块与内嵌的 ...
- 【shell脚本学习-4】
文本处理 #!/bin/bash#----------文本处理---------- #---------------echo----------------- # "-n":处理光 ...
- Hadoop Eclipse 插件制作以及安装
在本地使用Eclipse调试MapReduce程序,需要Hadoop插件,笔摘记录下制作安装过程. 准备工作(hadoop-2.6.0为例): 搭建好Hadoop环境 下载Hadoop安装包,解压到某 ...