HDU - 4725 (The Shortest Path in Nya Graph)层次网络
题意:有n个点,每个点都在一个层内,层与层之间的距离为c,一个层内的点可以到达与它相邻的前后两个层的点,还有m条小路
。。时间真的是卡的很恶心啊。。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = , INF = 0xfffffff;
int n,m,c;
struct node{
int u, v, d, next;
}Node[maxn*];
int d[maxn], vis[maxn], head[maxn], tmp[maxn], vv[maxn];
void add(int u,int v,int d,int i)
{
Node[i].u = u;
Node[i].v = v;
Node[i].d = d;
Node[i].next = head[u];
head[u] = i;
} void spfa(int s)
{
queue<int> Q;
mem(vis,);
fill(d,d+maxn,INF);
d[s] = ;
Q.push(s);
vis[s] = ;
while(!Q.empty())
{
int x = Q.front();Q.pop();
vis[x] = ;
for(int i=head[x]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] > d[x] + e.d)
{
d[e.v] = d[x] + e.d;
if(!vis[e.v])
{
Q.push(e.v);
vis[e.v] = ;
}
}
}
} } int main()
{
int T;
int res = ;
scanf("%d",&T);
while(T--)
{
int ans = ;
mem(tmp,);
mem(vv,);
mem(head,-);
scanf("%d%d%d",&n,&m,&c);
for(int i=; i<=n; i++)
{
scanf("%d",&tmp[i]);
vv[tmp[i]] = ; //若这层有点 则标记
}
for(int i=;i<=n;i++)
{
if(vv[i] && vv[i+]) //判断相邻两层是否有点 若有 则连接相邻两层
{
add(n+i,n+i+,c,ans++);
add(n+i+,n+i,c,ans++);
}
}
for(int i=;i<=n;i++)
{
add(n+tmp[i],i,,ans++); // 连接层与点
if(tmp[i] > ) add(i,n+tmp[i]-,c,ans++); //连接点与相邻层
if(tmp[i] < n) add(i,n+tmp[i]+,c,ans++);
}
for(int i=; i<m; ++i) //连接点与点
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
add(u,v,d,ans++);
add(v,u,d,ans++);
}
spfa();
printf("Case #%d: ",++res);
if(d[n]!=INF)
printf("%d\n",d[n]);
else
printf("-1\n"); } return ;
}
HDU - 4725 (The Shortest Path in Nya Graph)层次网络的更多相关文章
- 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 ...
- 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 ...
- 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 【拆点 + dijkstra】
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
随机推荐
- 浅谈Java泛型中的extends和super关键字
泛型是在Java 1.5中被加入了,这里不讨论泛型的细节问题,这个在Thinking in Java第四版中讲的非常清楚,这里要讲的是super和extends关键字,以及在使用这两个关键字的时候为什 ...
- jsp界面的继承与否剖析
引入页面时候 ${pageContext.request.contextPath}为页面上下文路径:也可以用js来实现: <script type="text/javascript&q ...
- P3830 [SHOI2012]随机树
P3830 [SHOI2012]随机树 链接 分析: 第一问:f[i]表示有i个叶子结点的时候的平均深度,$f[i] = \frac{f[i - 1] + 2 + f[i - 1] * (i - 1) ...
- (一)ABP添加控制器和页面(有时候页面不出来)
1:添加控制器后需要写[Area("AppAreaName")] 2:继承 WebControllerBase 3:创建视图就可以出现index页面了
- 个人博客作业Week2(代码规范,代码复审)
Q:是否需要有代码规范 首先我们来搞清楚什么是“代码规范”,它和“代码风格”又有什么关系.依据个人的审美角度,我可能更喜欢在函数与函数之间空出一行,可能在命名习惯和代码注释上更加的internatio ...
- Linux实践:模块
标签(空格分隔): 20135321余佳源 一.实践原理 Linux模块是一些可以作为独立程序来编译的函数和数据类型的集合.之所以提供模块机制,是因为Linux本身是一个单内核.单内核由于所有内容都集 ...
- pl/sql破解方法
转载源:http://blog.csdn.net/oscar999/article/details/2123803 打开注册表在run下输入regedit删除1.HKEY_CURRENT_USER/S ...
- 非post请求时整个url作为参数传递出现bug
在非post请求使用整个url作为参数传递到后台时会出现url被截断的bug,这时通过encodeURIComponent进行url的编码可以解决.示例如下: <!--参数url-->Ur ...
- 关于RESTful 的概念
1.REST 是面向资源的,这个概念非常重要,而资源是通过 URI 进行暴露.URI 的设计只要负责把资源通过合理方式暴露出来就可以了.对资源的操作与它无关,操作是通过 HTTP动词来体现,所以RES ...
- .NET 使用 RabbitMQ 图文简介
前言 最近项目要使用RabbitMQ,园里里面已经有很多优秀的文章,Rabbitmq官网也有.net实例.这里我尝试下图文并茂之形式记录下使用的过程. 安装 RabbitMQ是建立在erlang OT ...