题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725

  如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n+i。n+i节点向 i 层的所有连边,权值为0。i 层的所有点向2*n+i节点连边,权值为0。然后每层直接建立边就可以了,即2*n+i-1向n+i连边,权值为c,2*n+i向n+i-1连边,权值为c。3*n个点,最多有有9*n条边。。

 //STATUS:C++_AC_730MS_14340KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e60;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v,w;
}e[*N];
int first[N],next[*N];
LL d[N];
int S,T,n,m,c,mt; void adde(int a,int b,int c)
{
e[mt].u=a,e[mt].v=b;e[mt].w=c;
next[mt]=first[a],first[a]=mt++;
}
#define pli pair<LL,int>
LL dijkstra(int s)
{
int i,j,u,v,x;
pli t;
priority_queue<pli,vector<pli>,greater<pli> > q;
for(i=;i<=*n;i++)d[i]=LNF;
d[s]=;
q.push(make_pair(d[s],s));
while(!q.empty()){
t=q.top();q.pop();
u=t.second;
if(t.first!=d[u])continue;
for(i=first[u];i!=-;i=next[i]){
if(d[u]+e[i].w<d[e[i].v]){
d[e[i].v]=d[u]+e[i].w;
q.push(make_pair(d[e[i].v],e[i].v));
}
}
}
return d[T];
} int main(){
// freopen("in.txt","r",stdin);
int Ca,i,j,k,a,b,w,ca=;
scanf("%d",&Ca);
while(Ca--)
{
scanf("%d%d%d",&n,&m,&c);
S=,T=n;
mem(first,-);mt=;
for(i=;i<=n;i++){
scanf("%d",&a);
adde(n+a,i,);
adde(i,(n<<)+a,);
}
for(i=;i<=n;i++){
adde((n<<)+i-,n+i,c);
adde((n<<)+i,n+i-,c);
}
for(i=;i<m;i++){
scanf("%d%d%d",&a,&b,&w);
adde(a,b,w);
adde(b,a,w);
}
dijkstra(S);
if(d[T]==LNF)d[T]=-; printf("Case #%d: %I64d\n",ca++,d[T]);
}
return ;
}

HDU-4725 The Shortest Path in Nya Graph 最短路的更多相关文章

  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

    he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...

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

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

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

  7. (中等) 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 ...

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

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

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

随机推荐

  1. ipc$漏洞利用实例

    一.什么是IPC$ IPC$(Internet Process Connection)是共享"命名管道"的资源,它是为了让进程间通信而开放的命名管道, 通过提供可信任的用户名和口令 ...

  2. .NET 框架 (转载)

    转载:http://www.tracefact.net/CLR-and-Framework/DotNet-Framework.aspx .NET框架 三年前写的<.NET之美>的第六章,现 ...

  3. 简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。

    简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现. 题目: Suppose a sorted array is rotated at som ...

  4. DNS子域委派配置案例[转载]

    最近在研究linux dns 在51上面看见这篇文章,感觉讲的很透彻,随转载,方便以后自己查阅 原文地址:http://www.51osos.com/a/Linux_CentOS_RedHat/Lin ...

  5. Segmentation Fault错误原因总结

    最近在项目上遇到了Segmentation Fault的错误,一直调试不出来是哪里出了问题,对于刚接触嵌入式的,也不知道该如何去调试一个项目,定位内存问题,纠结了好几天,好阿红整理下自己的思路.从头开 ...

  6. Struts2系列——struts2的result

    在action的指定方法执行完毕后总会返回一个字符串,struts2根据返回的字符串去action的配置中的result去找匹配的名字,根据配置执行下一步的操作. 在ActionSupport基类中定 ...

  7. UVa 10088 (Pick定理) Trees on My Island

    这种1A的感觉真好 #include <cstdio> #include <vector> #include <cmath> using namespace std ...

  8. 微信分享自定义图片标题摘要-微信官方API

    我们平时在使用微信内置浏览器打开网页想要分享给好友或者发到朋友圈的时候经常会遇到这样的问题, 别人的网页分享的时候是这样的: 而我们自己的网页分享后这这样的: 看到有人说不做任何设置,微信分享时会自动 ...

  9. Java Web编程的主要组件技术——Hibernate入门

    参考书籍:<J2EE开源编程精要15讲> Hibernate是对象/关系映射(ORM,Object/Relational Mapping)的解决方案,就是将Java对象与对象关系映射到关系 ...

  10. 从投影的角度理解pca:向量,投影,基,内积,坐标,维数,分散程度,方差,协方差矩阵,对角化,特征值分解,主成分分析PCA

    参考:http://blog.csdn.net/songzitea/article/details/18219237