很有挑战的一题 直接暴力建图的话毫无疑问O(n^2)会TLE 每层虚拟一个点又会让没有点的层也能连过去

参考kuangbin菊苣的方法每层用了两个虚拟点 n+i*2-1 是入口 n+i*2 是出口 然后建单向边就可以了

VA了一次 因为MAXN应该比数据量大两倍 不小心忽略了 至于MAXM直接开到了1e7

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std; const int MAXN = 1e6 + ;
const int MAXM = 2e7 + ; typedef pair<int, int> pii;
struct cmp{
bool operator ()(const pii a, const pii b){
return a.first > b.first;
}
}; int size, head[MAXN], point[MAXM], nxt[MAXM], val[MAXM];
int t, n, m, c, dist[MAXN]; void init()
{
size = ;
memset(head, -, sizeof head);
} inline void add(int from, int to, int value)
{
val[size] = value;
point[size] = to;
nxt[size] = head[from];
head[from] = size++;
} void dij(){
priority_queue<pii, vector<pii>, cmp> q;
memset(dist, 0x3f, sizeof dist);
q.push(make_pair(, ));
dist[] = ;
while(!q.empty()){
pii u = q.top();
q.pop();
if(u.first > dist[u.second]) continue;
for(int i = head[u.second]; ~i; i = nxt[i]){
if(dist[point[i]] > dist[u.second] + val[i]){
dist[point[i]] = dist[u.second] + val[i];
q.push(make_pair(dist[point[i]], point[i]));
}
}
}
} int main()
{
scanf("%d", &t);
for(int kase = ; kase <= t; kase++){
init();
scanf("%d%d%d", &n, &m, &c);
for(int i = ; i <= n; i++){
int layer;
scanf("%d", &layer);
add(n + *layer - , i, );
add(i, n + *layer, );
}
for(int i = ; i < n; i++){
add(n + *i, n + *(i+) - , c);
add(n + *(i+), n + *i - , c);
}
int u, v, w;
for(int i = ; i <= m; i++){
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
} dij();
printf("Case #%d: %d\n", kase, dist[n] == INF ? - : dist[n]);
}
return ;
}

kuangbin_ShortPath P (HDU 4725)的更多相关文章

  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(最短路拆点)

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

  4. HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C.还有M ...

  5. HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...

  6. HDU 4725 建图

    http://acm.hdu.edu.cn/showproblem.php?pid=4725 The Shortest Path in Nya Graph Time Limit: 2000/1000 ...

  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(建图+优先队列dijstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有n个点和n层,m条边,每一层的任意一个点都可以花费固定的值到下一层或者上一层的任意点 然 ...

  9. AC日记——The Shortest Path in Nya Graph hdu 4725

    4725 思路: 拆点建图跑最短路: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...

随机推荐

  1. 测试V模型

    一:测试V模型 RAD(Rap Application Development 快速引用开发)模型是软件开发过程中的一个重要模型,由于模型构图形似字母V,所以又称软件开发的V模型.他通过开发和测试同时 ...

  2. jQuery Ajax之load()方法

    jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是load().$.get()和$.post()方法,第3层是$.getScript()和$.getJ ...

  3. SharePoint安全 - SharePoint网站常用页面URL索引

    博客地址 http://blog.csdn.net/foxdave 一. 主要网站内容 首页 /default.aspx /Pages/default.aspx 网站设置 /_layouts/sett ...

  4. 统计一段文字中出现频率最高的10个单词(c语言)

    注:这次使用C语言做的这个程序.个别不懂的地方和算法部分是请教的其他同学,交流并吸收,所以收获颇多! 在程序中每一个地方我都做了注释,方便同学之间交流.也让老师容易看.程序也有很多不足的地方,但限于本 ...

  5. IOS中nil/Nil/NULL的区别

    类与对象的概念 类是对同一类事物高度的抽象,类中定义了这一类对象所应具有的静态属性(属性)和动态属性(方法). 对象是类的一个实例,是一个具体的事物. 类与对象是抽象与具体的关系. 类其实就是一种数据 ...

  6. 如何用JS判断网页中某个id的网页元素是否存在

    <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <m ...

  7. Qemu文档

    http://wiki.qemu.org/Manual http://qemu.weilnetz.de/qemu-doc.html http://www.linuxcertif.com/man/1/q ...

  8. wMy_Python ~储存相关~

    str,int,list,tuple,dict 是类型调用之后会产生一个 实例 >>> brand=["李宁",'耐克','阿迪达斯','鱼C'] >> ...

  9. BZOJ 3450 Easy

    注意细节啊... 和上一道差不多. #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  10. Unity3d各平台资源路径文件夹

    之前一直是PC项目,公司终于考虑移动平台了,但是试验了几把,感觉移动平台资源管理路径还是有很多隐藏的注意事项. 比如在PC上可以做到随便读写,但是在移动平台就涉及到权限问题. 看到小伙伴的总结,还是要 ...