The Shortest Path in Nya Graph---hdu4725(spfa+扩点建图)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4725
有n个点,每个点都有一个层l[i],相邻层的边有一条无向带权边,权值为都为C,另外还有m条边,每条边对应的u v w
最后求1到n的最小权值和是多少;
如果直接建图的话会TLE;这里把层数扩展为点n+1----n+n;然后在连接各种关系对应的图,最后用spfa求最短路即可,注意扩点之后点的个数;
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <string>
typedef long long LL;
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
#define N 200005 using namespace std; struct node
{
int v, w, Next;
}G[N*]; int n, m, c, dist[N], vis[N], l[N], v[N]; int Head[N], cnt;
void Add(int u, int v, int w)
{
G[cnt].v = v;
G[cnt].w = w;
G[cnt].Next = Head[u];
Head[u] = cnt++;
} int spfa()
{
for(int i=; i<=n*+; i++)
dist[i] = INF;
met(vis, );
queue<int>Q;
Q.push();
vis[] = ;
dist[] = ;
while(!Q.empty())
{
int p = Q.front();Q.pop();
vis[p] = ;
for(int i=Head[p]; i!=-; i=G[i].Next)
{
int q = G[i].v;
if(dist[q] > dist[p]+G[i].w)
{
dist[q] = dist[p]+G[i].w;
if(!vis[q])
{
vis[q] = ;
Q.push(q);
}
}
}
}
return dist[n];
} int main()
{
int T, t = ;
scanf("%d", &T);
while(T--)
{
met(Head, -);
met(v, );
cnt = ; scanf("%d %d %d", &n, &m, &c); for(int i=; i<=n; i++)
{
scanf("%d", &l[i]);
v[l[i]] = ;
} for(int i=; i<n; i++)
{
if(v[i] && v[i+])
{
Add(n+i, n+i+, c);///层与层之间连边;
Add(n+i+, n+i, c);
}
}
for(int i=; i<=n; i++)
{
Add(n+l[i], i, );///当前点与它所在的层连边,边为0;
if(l[i]!=) Add(i, n+l[i]-, c);///点和层间进行连边;
if(l[i]!=n) Add(i, n+l[i]+, c);
} for(int i=; i<=m; i++)
{
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
Add(u, v, w);
Add(v, u, w);
}
int ans = spfa();
if(ans == INF) ans = -;
printf("Case #%d: %d\n", t++, ans);
}
return ;
}
The Shortest Path in Nya Graph---hdu4725(spfa+扩点建图)的更多相关文章
- HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...
- ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)
职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- HDU4725: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 (拆点+dji)
HDU 4725 The Shortest Path in Nya Graph : http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意: 在一个图中跑最 ...
- 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 ...
随机推荐
- String,你到底创建了几个对象????
String str=new String("aaa"); 这行代码究竟创建了几个String对象呢?答案是2个,而不是3个.由于 new String("aaa&quo ...
- cSS3 伪类:nth-child 的使用方法
:nth-child是一个非常牛逼的伪类,如果你能很好的理解它就可以用CSS 做出很多非常实用的效果.当我很年轻的时候还使用PHP的i++来实现一些东西,其实CSS 完全可以实现.下面是我总结的一些用 ...
- TYVJ P1020 寻找质因数
做题记录:2016-08-08 描述 给出N个数字,试求质因数最大的数字. 输入格式 第一行,一个整数N,表示数字个数.接下来N行,每行一个整数A_i,表示给出的数字. 输出格式 一个整数,表示质因数 ...
- 蒟蒻修养之tc蓝名计划
开一个新坑......(听说tc是智商高的人才能玩的QAQ显然我是被屠的... 1 [645DIV2]这个能说是裸模拟吗... 弃坑= =做了一些题感觉没必要放上来了= =等div1先吧....... ...
- Graph database_neo4j 底层存储结构分析(5)
3.5 Property 的存储 下面是neo4j graph db 中,Property数据存储对应的文件: neostore.propertystore.db neostore.propertys ...
- 一致性 hash 算法
consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛: 1 ...
- System.getProperty
我们可以通过System.getProperty("user.home")读取JAVA系统的user.home属性的值.
- php 数组操作
<?php $vegetables[0] = "corn"; $vegetables[1] = "broccoli"; $vegetables[2] = ...
- CSS权威指南 - 浮动和定位 2
定位 定位的想法很简单元素框相对于正常位置出现在哪里. 定位:static,相对, 绝对, fixed, 继承 static就是默认的位置 相对就是相对于默认位置的偏移.原来的static定位位置依然 ...
- Redis 笔记与总结6 Redis 高级应用之 事务处理、持久化操作、pub_sub、虚拟内存
3.事务处理 redis 对事务的支持目前还比较简单. redis 只能保证一个 client 发起的事务中的命令可以连续的执行,而中间不会插入其他 client 的命令. 由于 redis 是单线 ...