题目链接: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. apache&nginx资料汇总

    http://liudaoru.iteye.com/blog/336338 aquid:http://os.51cto.com/art/201009/225813.htm 数据库各种讲座:http:/ ...

  2. PHP输出缓冲控制- Output Control 函数应用详解

    说到输出缓冲,首先要说的是一个叫做缓冲器(buffer)的东西.举个简单的例子说明他的作用:我们在编辑一篇文档时,在我们没有保存之前,系统是不会向磁盘写入的,而是写到buffer中,当buffer写满 ...

  3. Linux3.4内核的基本配置和编译

    转载自:http://www.embedu.org/Column/Column634.htm 作者:李昕,华清远见研发中心讲师. 了解Linux3.4内核的特性及新增功能,掌握Linux内核的编译过程 ...

  4. HBase学习笔记

    关键类: HBaseAdmin 管理Hbase的,主要负责DDL操作 HTable 管理表中数据,主要负责DML操作 1.为了避免热点,更多的建表方法 在Shell中: },{SPLITS=>[ ...

  5. java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataProvider

    Caused by: java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvi ...

  6. ASCII码常用值

    大写字母 A~Z 65~90 小写字母a~z 97~122 数字0~9 48~ 57

  7. WPF 用 DataTemplate 合并DataGrid列表列头<类似报表设计>及行头列头样式 - 学习

    WPF中 DataGrid 列头合并,类似于报表设计.效果图如下↓ 1.新建一个WPF项目WpfApplication1,新建一个窗体DataGridTest,前台代码如下: <Window x ...

  8. 在Windows下利用php自带的mail函数发邮件

    这几天看<Head First PHP & MySQL>,里面有发邮件的例子是用系统自带的mail函数发送的,自己照书上写的试了一直不成功,后来终于在网上找到解决方案,现在总结下. ...

  9. windows2003 IIS6网络负载平衡设置

    问题 随着计算机技术的不断发展,单台计算机的性能和可靠性越来越高.但现实中还是有许多应用是单台计算机难以达到,例如: 1.银行存储用户数据的数据库服务器必须保证24小时不间断的运转,并在发生严重硬件故 ...

  10. 页面多个Jquery版本共存的冲突问题,解决方法!

    示例如下: <script type="text/javascript" src="jquery.js"></script> <s ...