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.
 
  题意就是求最短路问题,但是有一个层的概念,刚开始的时候是想着把相邻两层之间的每一点都再连一条边,但是这样的复杂度可以到达 n^2 ,比如在相邻两层上都有 n/2 个点。
  然后百度了之后,发现是要增加点,表示每一层,这个点和同层的连边的边权为0,这样的话两层之间就只需要再加一条边,但是这样还有一个问题,就是同层点之间来往的话,应该是到达相邻的层然后再回来,所以需要在每一层增加两点,分别表示入的和出的。。。
  还有一个坑点,就是如果某一层没有点的话,是不能与相邻层连线的。。。
 
代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=*;
const int MaxM=*;
const int INF=10e9+; struct Edge
{
int to,next,cost;
}; struct Node
{
int v,val; Node(int _v=,int _val=):v(_v),val(_val) {} bool operator < (const Node &a) const
{
return val>a.val;
}
}; Edge E[MaxM];
int head[MaxN],Ecou; void init(int N)
{
Ecou=; for(int i=;i<=N;++i)
head[i]=-;
} void addEdge(int u,int v,int c)
{
E[Ecou].to=v;
E[Ecou].cost=c;
E[Ecou].next=head[u];
head[u]=Ecou++;
} bool vis[MaxN]; void Dijkstra(int lowcost[],int N,int start)
{
priority_queue <Node> que;
Node temp;
int u,v,c;
int len; for(int i=;i<=N;++i)
{
lowcost[i]=INF;
vis[i]=;
} lowcost[start]=;
que.push(Node(start,)); while(!que.empty())
{
temp=que.top();
que.pop(); u=temp.v; if(vis[u])
continue; vis[u]=; for(int i=head[u];i!=-;i=E[i].next)
{
v=E[i].to;
c=E[i].cost; if(!vis[v] && lowcost[v]> lowcost[u]+c)
{
lowcost[v]=lowcost[u]+c;
que.push(Node(v,lowcost[v]));
}
}
}
} int ans[MaxN];
bool have[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T;
int N,M,C;
int u,v,c;
int cas=; scanf("%d",&T); while(T--)
{ scanf("%d %d %d",&N,&M,&C); init(*N);
memset(have,,sizeof(have)); for(int i=;i<=N;++i)
{
scanf("%d",&u); addEdge(i,N+*u-,);
addEdge(N+*u,i,); have[u]=;
} for(int i=;i<N;++i)
if(have[i] && have[i+])
{
addEdge(N+*i-,N+*(i+),C);
addEdge(N+*(i+)-,N+*i,C);
} for(int i=;i<=M;++i)
{
scanf("%d %d %d",&u,&v,&c); addEdge(u,v,c);
addEdge(v,u,c);
} Dijkstra(ans,*N,); printf("Case #%d: %d\n",cas++,ans[N]==INF ? - : ans[N]);
} return ;
}

(中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。的更多相关文章

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

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

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

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

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

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

  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(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

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

  9. HDU - 4725 The Shortest Path in Nya Graph 【拆点 + dijkstra】

    This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...

随机推荐

  1. swool安装

    swoole安装 安装完PHP后,即可安装swoole扩展. swoole扩展下载地址:https://github.com/swoole/swoole-src/releases 尽量选择stable ...

  2. 非常简单的oracle和mysql数据互传

    工具是navicat,我用的是Navicat Premium 10: 这个工具可以同时连接mysql和oracle,如图: 同时连接上这两个库之后 工具->数据传输 左边是数据源,右边是导入目标 ...

  3. AutoTile 自动拼接(二) 学习与实践

    开始代码前,我们要做点准备工作. 下面 跟着我做. 首先我 扣了一个 图. 这个是 做 水的资源,所以是动态的,我把其余两张也扣了出来. 看起来一样,不是,这样看肯定 看不出所以然,你们先放到u3d中 ...

  4. lucene 中关于Store.YES 关于Store.NO的解释

    总算搞明白 lucene 中关于Store.YES  关于Store.NO的解释了 一直对Lucene Store.YES不太理解,网上多数的说法是存储字段,NO为不存储. 这样的解释有点郁闷:字面意 ...

  5. PADS 导Gerber文件

    PCB也画了好几年,投板时都是直接发PCB文件,突然间客户让我导出Gerber文件, 一时半会还挺棘手的,上网不停的搜啊搜啊,虽然最终还是搞定了,但耽误了不少时间. 现总结下,把所有相关设置一步一步的 ...

  6. 测试redis+keepalived实现简单的主备切换【转载】

    转自: 测试redis+keepalived实现简单的主备切换 - Try My Best 尽力而为 - ITeye技术网站http://raising.iteye.com/blog/2311757 ...

  7. JavaBean--删除操作

    删除命令:removeAttribute(Javabean名称) 前面调用用pageContext,request,session,application, 如request.removeAttrib ...

  8. Android应用性能测试之CPU和内存占用

    最近发现自己学的很多东西没有做好积淀的工作,也萌生了写一些东西的念头.本人也没有写博客的习惯,下边就写一下手机端的性能测试. 最近公司,要我们从事对竞品的性能测试,我负责CPU和内存的性能测试,下面就 ...

  9. 把一个 int 数字 n 格式化成16进制的字符串(前面补零成0位)

    例如,输入n=10,要求输出 0x0000000A; C++:  sprintf( buffer, "0x%08X", n); C#:    string s = string.F ...

  10. Myeclipse或Eclipse中搭建Easyui环境

    1.下载Easyui.网址:http://www.jeasyui.com/download/index.php 2.下载后解压,里面的demo文件夹可以不用添加到工程中. 3.如图所示在工程datag ...