很有挑战的一题 直接暴力建图的话毫无疑问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. 大开眼界 游览Facebook香港办公室

    想加入Facebook 的话不一定要跑去美国,Facebook在香港也开了一个很赞的办公室.除了无敌海景外,更可享用按摩椅.乒乓球桌.跑步机.麻将桌.酒廊.育婴室及开放式厨房.

  2. SharePoint开发 - Excel数据导入到SharePoint自定义列表(数据视图方式)

    博客地址 http://blog.csdn.net/foxdave 本篇讲解一个有些新颖的SharePoint实例应用,给甲方做过项目的都有过体会,数据太多了,客户有Excel,要求实现批量导入. 效 ...

  3. <转>用thinkPHP实现验证码的功能

    许多系统的登录都有验证码,而如果使用thinkPHP框架搭建网站的话,验证码的生成和验证就比较容易了 1.生成验证码 thinkPHP有对应生成验证码的方法 要使用验证码,需要导入扩展类库中的ORG. ...

  4. <二叉树的基本操作(有层次遍历)>

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define num 100 #define OK ...

  5. poj2392 多重背包

    //Accepted 868 KB 188 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...

  6. form表单验证2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Interview----判断两个链表是否相交?

    题目描述: 判断两个单链表是否相交?假设链表没有环. 假如链表有环呢? 1.  假如没有环 那么如果两个链表相交的话,必然最后的节点一定是同一个节点.所以只需要各自扫描一遍链表,找到最后一个节点,比较 ...

  8. Python ~~~ 面向对象的利器

    class Rectangle(): # 有没有括号都行 . def __init__(self,x,y): self.x=x self.y=y def getPeri(self): def getA ...

  9. Some SQL basics

    1, Index An index is a set of data pointers stored on disk associated with a single table. The main ...

  10. linux 下安装rsync

    一.服务器端配置: 1.安装xinetd,并修改rsync相关配置 # yum -y install xinetd # vi /etc/xinetd.d/rsync 如下代码: service rsy ...