The Shortest Path in Nya Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4872    Accepted Submission(s): 1122

Problem 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 <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), 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
 
 
 
题意: 有n个地点,m条边,每条边耗费w,每个点又属于一些集合,相邻的集合之间的点可以直接通行,耗费c。现在求1到n的最少费用。
 
思路:可以把集合当做一个点,从集合到集合内的点费用为0,点到相邻的集合费用为c,这样就既保证了不用暴力枚举相邻集合里面的每个点,有保证相邻集合里的点的最短路(表达能力不好 -。-#!)。
就是这张图的意思:
然后一遍dij+heap就好。

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<time.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
struct node
{
int to;
int val;
int next;
}edge[MAXN * ];
int pre[MAXN*],vis[MAXN*],dis[MAXN*],n,m,k,c,ind;
int num[MAXN],ok[MAXN];
void add(int x,int y,int z)
{
edge[ind].to = y;
edge[ind].val = z;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
struct Node
{
int p;
ll val;
Node(){}
Node(int fp,int fval):p(fp),val(fval){}
friend bool operator < (Node fa,Node fb){
return fa.val > fb.val;
}
};
void dij(int s)
{
priority_queue<Node>fq;
for(int i = ; i <= k; i++){
dis[i] = INF;
vis[i] = ;
}
dis[s] = ;
fq.push(Node(s,));
while(!fq.empty()){
Node tp = fq.top();
fq.pop();
vis[tp.p] = ;
for(int i = pre[tp.p]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!vis[t] && dis[t] > dis[tp.p] + edge[i].val){
dis[t] = dis[tp.p] + edge[i].val;
fq.push(Node(t,dis[t]));
}
}
}
}
int main()
{
int t,ff = ;
scanf("%d",&t);
while(t--){
memset(num,,sizeof(num));
memset(ok,,sizeof(ok));
scanf("%d%d%d",&n,&m,&c);
int x;
ind = ;
memset(pre,-,sizeof(pre));
for(int i = ; i <= n; i++){
scanf("%d",&x);
ok[x] = ;
num[i] = x;
}
for(int i = ; i <= n; i++){//点与集合之间连边
add(n+num[i],i,);
if(num[i] > )add(i,n+num[i]-,c);
if(num[i] < n)add(i,n+num[i]+,c);
}
int y,z;
for(int i = ; i <= m; i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
//cout<<k<<endl;
k = * n;
dij();
printf("Case #%d: ",++ff);
if(dis[n] >= INF){
printf("-1\n");
}
else {
printf("%d\n",dis[n]);
}
}
return ;
}
 

hdu4725最短路变形 添加点的更多相关文章

  1. POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)

    做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...

  2. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  3. POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...

  4. POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K          Description Background  Hugo ...

  5. HDOJ find the safest road 1596【最短路变形】

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. HN0I2000最优乘车 (最短路变形)

    HN0I2000最优乘车 (最短路变形) 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! [试题]为了简化城市公共汽车收费系 ...

  7. 天梯杯 PAT L2-001. 紧急救援 最短路变形

    作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...

  8. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

  9. POJ 2253 Frogger ( 最短路变形 || 最小生成树 )

    题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 Kruskal 算法,在建树的过程中贪心的从最小的边一个 ...

随机推荐

  1. Java之反射机制

    一:基本概念:在Java运行时,对于任意一个类,能否知道这个类对应的属性和方法?对于一个对象,能否知道可以调用它的哪些方法?YES! 这种动态获取类的信息以及动态调用对象的方法的功能来自于Java语言 ...

  2. LinkedList子类与Queue接口

    LinkedList表示的是一个链表的操作类.定义如下: public class LinkedList<E> extends AbstractSequentialList<E> ...

  3. django复习笔记1:环境配置

    一.IDE 推荐使用sublime安装djaneiro插件. 1.安装方式 package control中搜索djaneiro 支持补全请参考:Django support for Sublime ...

  4. varnish 的一个配置

    backend default { .host = "10.32.26.31"; .port = "; } sub vcl_recv { if (req.url ~ &q ...

  5. java多线程系类:基础篇:09之interrupt()和线程终止方式

    概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于"阻塞状态"的线程2.2 ...

  6. 安装Ubuntu时的硬盘分区方案

    如果你准备在硬盘里只安装Ubuntu一个操作系统的话,建议你采用一个“/”.一个“swap”和一个“/home”的三分区方案:/ :10GB-15GB.swap:物理内存小于或等于 512MB,建议分 ...

  7. JavaScript 位运算总结&拾遗

    最近补充了一些位运算的知识,深感位运算的博大精深,此文作为这个系列的总结篇,在此回顾下所学的位运算知识和应用,同时也补充下前文中没有提到的一些位运算知识. 把一个数变为大于等于该数的最小的2的幂 一个 ...

  8. 20160223 - Windows 10 的文件资源管理器下出现重复文件夹的解决办法

    现象: 安装 OneDrive 从 Windows 7.8.8.1 升级来的 Windows 10 的电脑,可能会出现文件资源管理器左侧面板中出现重复的文件夹. 通常有:视频.图片.文档.下载.音频. ...

  9. bootstrap点滴

    1.nav-stacked 这个属性可以决定 tab的变为竖的,不添加的话为横向的. 2.tab  横向的 ul中必须含有nav nav-tabs ul li a 中必须有data-toggle=&q ...

  10. sql server 字符串函数str()

    语法: STR(nExpres[,nLength[,nDecimalPlaces]]) 参数: nExpression------STR要计算的数值表达式. nLength------------ST ...