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 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.
Sample Input
2
3 3 3
1 3 2
1 2 1
2 3 1
1 3 3
3 3 3
1 3 2
1 2 2
2 3 2
1 3 4
Sample Output
Case #1: 2
Case #2: 3
这题的题意是有1-n个点,分布在1-n的若干层上,一层上有可能很多的点,也可能没有点。两个相邻的层上的点可以花费C连通,除此之外还有m条边。求从点1到点n的最短路径。
这题其实就是构造,因为相邻的层之间的点可以建权重是C的边,如果点i在r层,那么假设层r所在的点是r+n,实际上就是建立从点i到点r+n的权重为0的有向边,当有点j位于第r+1层或者r-1层时,实际上就是建立从点r+n到点j的权重为C的有向边。最后去做一个ElogE的Dijkstra就行了。
这题要注意的就是把层抽象化成点之后,点的个数实际上是多了一倍,开数组的时候一定要记得乘2。(没有*2哇了两个小时(泣))
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
using namespace std;
typedef long long ll; const int maxn = 1e5+;
const int N = 1e4+;
const int mol = 1e9+;
int arr[maxn],l[maxn],r[maxn],vis[N];
vector <int> vi[N]; int main()
{
for(int i=;i<N;i++)
for(int j=;j<=sqrt(i);j++)
if(i%j == )
{
vi[i].push_back(j);
if(j*j != i) vi[i].push_back(i/j);
}
int n;
while(~scanf("%d",&n))
{
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(vis,,sizeof(vis));
ll ans = ;
for(int i=;i<=n;i++)
scanf("%d",&arr[i]);
for(int i=;i<=n;i++)
{
int tp = ;
for(int j=;j<vi[arr[i]].size();j++)
tp = max(tp,vis[vi[arr[i]][j]]);
l[i] = tp;
//cout << tp << " ";
vis[arr[i]] = i;
}
//cout << endl;
for(int i=;i<N;i++) vis[i] = n+;
for(int i=n;i>;i--)
{
int tp = n+;
for(int j=;j<vi[arr[i]].size();j++)
tp = min(tp,vis[vi[arr[i]][j]]);
//cout << tp << " ";
r[i] = tp;
vis[arr[i]] = i;
}
//cout << endl;
for(int i=;i<=n;i++)
ans = (ans + 1LL*(i-l[i])*(r[i]-i) % mol) % mol;
printf("%lld\n",ans);
}
}
HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]的更多相关文章
- 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 (最短路 )
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
- HDU 4725 The Shortest Path in Nya Graph(最短路拆点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:n个点,某个点属于某一层.共有n层.第i层的点到第i+1层的点和到第i-1层的点的代价均是 ...
- HDU 4725 The Shortest Path in Nya Graph(最短路建边)题解
题意:给你n个点,m条无向边,每个点都属于一个层,相邻层的任意点都能花费C到另一层任意点,问你1到n最小路径 思路:没理解题意,以为每一层一个点,题目给的是第i个点的层数编号.这道题的难点在于建边,如 ...
- 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
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,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
随机推荐
- 原生js模拟双色球
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- POJ 2377 Bad Cowtractors( 最小生成树转化 )
链接:传送门 题意:给 n 个点 , m 个关系,求这些关系的最大生成树,如果无法形成树,则输出 -1 思路:输入时将边权转化为负值就可以将此问题转化为最小生成树的问题了 /************* ...
- Linux(CentOS 6.4)系统中安装mplayer
整了一个上午终于把mplayer安装上了,我的系统是centos 6.4,真是不容易啊! 一.准备工作 需要的安装包及下载地址:1.mplayer源代码包(MPlayer-1.0rc4.tar.bz2 ...
- C# 实现自定义的USB设备与上位机进行通信(上位机部分)
因为以前没用过USB,对USB也不了解,于是上网查了很多资料,不过网上的资料都是零零散散,不清不楚的,于是我自己总结了一下,下面几个链接是网上这么多零散资料里,我觉得比较有参考意义的. USB设备连接 ...
- TOMCAT-IDEA远程debug方法
在很多情况下,tomcat本地启动并不足以完全模拟线上环境,所以,有时候我们可能需要远程debug方法去调试,下面附上远程idea debug方法: IDEA中,选择 Run/Debug Config ...
- php strtotime 同样的函数为何在不同的地方输出的结果不同?
方法1:调用函数 date_default_timezone_set('Asia/Shanghai'); // 如果是中国的话 方法2:设置php.ini 中data.timezone [Date] ...
- 【hiho一下 第三周】KMP算法
[题目链接]:http://hihocoder.com/problemset/problem/1015 [题意] [题解] 把f数组,len1,len2数组一开始全都定义成char型 这酸爽. [Nu ...
- http协议各版本的区别
HTTP(Hypertext transfer protocol)超文本传输协议,是一个应用层的通信协议. HTTP协议版本介绍: HTTP/0.9 :只接受GET一种请求方法,没有在通信中指定版本号 ...
- 2015 Multi-University Training Contest 7 hdu 5378 Leader in Tree Land
Leader in Tree Land Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- mysql 新用户添加和权限
1进入数据库 首先,启动数据库服务, sudo service mysql start2. 添加密码 因为MySQL的root用户默认是没有密码,所以直接连接.但作为最该权限用户,没有秘密是绝对不安全 ...