分析:对于每一层,原来n个点,然后扩展为原来的三倍,每一层扩展一个入点,一个出点,然后跑最短路

注:tmd我把一个n写成m了,然后wa了7次,我都要怀疑人生了

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=1e5+;
const int INF=0x3f3f3f3f;
struct Edge{
int v,w,next;
bool operator<(const Edge &e)const{
return w>e.w;
}
}edge[N*];
int head[N*],tot,n,m,c,d[N*];
void add(int u,int v,int w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
priority_queue<Edge>q;
bool vis[N*];
int dij(int s,int t){
for(int i=;i<=n;++i)d[i]=INF,vis[i]=;
d[s]=,q.push(Edge{s,,});
while(!q.empty()){
while(!q.empty()&&vis[q.top().v])q.pop();
if(q.empty())break;
int u=q.top().v;
q.pop();
vis[u]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(!vis[v]&&d[v]>d[u]+edge[i].w){
d[v]=d[u]+edge[i].w;
q.push(Edge{v,d[v],});
}
}
}
return d[t]==INF?-:d[t];
}
vector<int>g[N];
int main(){
int T,cas=;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&c);
for(int i=;i<=n;++i)g[i].clear();
for(int i=;i<=n;++i){
int x;
scanf("%d",&x);
g[x].push_back(i);
}
memset(head,-,sizeof(head));
tot=;
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);
}
for(int i=;i<=n;++i){
if(!g[i].size())continue;
if(i>&&g[i-].size())
add(n+i,*n+i-,c);
if(i<n&&g[i+].size())
add(n+i,*n+i+,c);
for(int j=;j<g[i].size();++j)
add(g[i][j],i+n,),add(i+*n,g[i][j],);
}
n*=;
printf("Case #%d: %d\n",++cas,dij(,n/));
}
return ;
}

HDU4725 The Shortest Path in Nya Graph dij的更多相关文章

  1. 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 题意: 在一个图中跑最 ...

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

  3. HDU4725 The Shortest Path in Nya Graph SPFA最短路

    典型的最短路问题,但是多了一个条件,就是每个点属于一个layer,相邻的layer移动,如x层移到x+1层需要花费c. 一种显而易见的转化是我把这些边都建出来,但是最后可能会使得边变成O(n^2); ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...

  5. hdu4725 The Shortest Path in Nya Graph【最短路+建图】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html      ---by 墨染之樱花 题目链接:http://acm.hdu ...

  6. hdu4725 The Shortest Path in Nya Graph

    这道题看了下很多人都是把每一层拆成两个点然后建图做的. 我的思路很直接,也不用建图,直接在更新每个点时更新他相邻的边和相邻的层,当然前提是每个点只更新一次,每个层也只更新一次,这样才能确保时间复杂度. ...

  7. 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)

    职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...

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

  9. 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 (J ...

随机推荐

  1. Keil 4 与Proteus 7.8联调

    实验环境: windows 8.1 pro with Keil 4 and Proteus 7.8 both cracked. 步骤: 下载联调工具Vdmagdi,安装. keil下Option/De ...

  2. ASP.NET MVC概述

    原文:http://www.asp.net/mvc/tutorials/older-versions/overview/asp-net-mvc-overview 这篇文章帮助你了解关于ASP.NET ...

  3. STM32F407移植contiki2.6后使用LWIP库实现tcp client

    最近在做智能家居,物联网项目,用到了C下的contiki移植 经过一阵调试,终于在 STM32F407移植contiki2.6后使用LWIP库实现tcp client. 一路艰辛谁人知道....唯有留 ...

  4. SQL学习_时间函数

    最近测试报表需要统计不同时间段的列表记录,收集一些时间函数作为参考,原文地址:http://blog.csdn.net/lyzlyfok/article/details/6282509 sql ser ...

  5. 使用Python编程语言连接MySQL数据库代码

    使用Python编程语言连接MySQL数据库代码,跟大家分享一下: 前几天我用python操作了mysql的数据库,发现非常的有趣,而且python操作mysql的方法非常的简单和快速,所以我把代码分 ...

  6. Silverlight之我见

    好长时间没搞Silverlight方面的开发了,原本都以为自己早已忘记,然而前阵子(确切一点说,是挺长时间以前了)的时候,发布Windows10的时候,微软宣布新的浏览器将重新开发,关键是后半句引起了 ...

  7. js事件之神奇的onclick

    21:58 2013/7/1 <a href="#" onclick="alert(123124);" >链接</a> 意思是鼠标点击链 ...

  8. 使用js为html元素动态添加class

    <ul id="root"> <li>1</li> <li>2</li> <li>3</li> ...

  9. Hadoop NameNode is not formatted.

    2014-08-26 20:27:22,712 WARN org.apache.hadoop.hdfs.server.namenode.FSNamesystem: Encountered except ...

  10. SpringMVC可以配置多个拦截后缀*.html和.do等

    一个servlet可以配置多个servlet-mapping, 因此在xml文件中我们可以这样配置: <!-- springmvc配置 --> <servlet> <se ...