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 

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 [构造 + 最短路]的更多相关文章

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

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

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

  4. HDU 4725 The Shortest Path in Nya Graph(最短路拆点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:n个点,某个点属于某一层.共有n层.第i层的点到第i+1层的点和到第i-1层的点的代价均是 ...

  5. HDU 4725 The Shortest Path in Nya Graph(最短路建边)题解

    题意:给你n个点,m条无向边,每个点都属于一个层,相邻层的任意点都能花费C到另一层任意点,问你1到n最小路径 思路:没理解题意,以为每一层一个点,题目给的是第i个点的层数编号.这道题的难点在于建边,如 ...

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

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

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

  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. Mybatis批量插入的代码实现

    简单的学习总结一下,希望能帮到需要的同学! 1.mapper.xml文件sql语句如下: <insert id="insertBatch" parameterType=&qu ...

  2. Python 从入门到实践 - Web应用程序

    一.创建项目 1.建立虚拟环境 python -m venv ll_env # 出现ll_env文件夹 2.激活虚拟环境 source ll_env/bin/activate # 要停止使用虚拟环境, ...

  3. 深入理解B/S与C/S架构

    首先来介绍一下B/S与C/S架构 C/S架构简要介绍 在了解什么是B/S架构之前,我们有必要了解一下什么是C/S架构: C/S架构是第一种比较早的软件架构,主要用于局域网内.也叫 客户机/服务器模式. ...

  4. 06006_redis数据存储类型——String

    1.概述 (1)字符串类型是Redis中最为基础的数据存储类型,它在Redis中是二进制安全的,这意味着该类型可以接受任何格式的数据,如JPEG图像数据或Json对象描述信息等: (2)在Redis中 ...

  5. NYIST 119 士兵杀敌(三)

    士兵杀敌(三)时间限制:2000 ms | 内存限制:65535 KB难度:5 描述南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进行比较,计算出 ...

  6. php 将多个txt文件合并成

    function test() { $hostdir= iconv("utf-8","gbk","C:\Users\原万里\Desktop\日常笔记& ...

  7. nodejs-路由(待补充)

    path Router 1 2 3 4 5 var express = require('express'); var Router = express.Router(); Router.get('/ ...

  8. 第一篇、Android Supersu 权限管理定制,隐藏过滤权限,指定APP最高权限

    近期有个需求,在预装ROM的时候,须要权限,可是又不同意全部的应用都有权限,仅仅同意自己的应用有最高的权限(当然没有系统签名情况下). 所以.编译了CM 提取了supersu进行了二次定制,让他进行权 ...

  9. nyoj-647-奋斗小蜗牛在请客(进制转换)

    奋斗小蜗牛在请客 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 一路艰辛一路收获.成功爬过金字塔的小蜗牛别提多高兴了.这不为了向以前帮助他的哥们们表达谢意,蜗牛宴请 ...

  10. $scope angular在controller之外调用

    1.定义 var m = angular.module('ddd',[]); m.controller('ctrl',['$scope',function ($scope) { }]); 2.外部调用 ...