hdu4725 The Shortest Path in Nya Graph【最短路+建图】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html ---by 墨染之樱花
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725
题目描述:给一张图,每个点(1~N)都属于一个层(1~N),比如点1在第5层,点3在第4层,点4也在第5层。任何在x层的点都可以移动到x+1层和x-1层中的任何点上,并且需要耗费C的权值。除此之外,另外再给出M条带权无向边。求点1到点N的最短路
思路:此图数据量比较大,暴力建图不可取。可以将层也抽象化成点,也就是一共有N个点节点和N个层节点,然后按照层与层之间(双向,权值C)、点与点之间(即后来给的M条边)、点与相对应的层之间(层指向点,权值0),点与对应层的相邻层之间(点指向层,权值C)建图,最后求最短路即可
#include <iostream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define XINF INT_MAX
#define INF 1<<30
#define MAXN 200000+10
#define eps 1e-8
#define zero(a) fabs(a)<eps
#define sqr(a) ((a)*(a))
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define PF(X) push_front(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define PI acos(-1.0)
#define test puts("OK");
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
typedef priority_queue<int,vector<int>,greater<int> > PQI;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second int V,M,C;
VII G[MAXN];
int d[MAXN];
bool cnt[MAXN];
int level[MAXN]; void addedge(int u,int v,int c)
{
G[u].PB(MP(v,c));
G[v].PB(MP(u,c));
} void dijkstra(int s)
{
priority_queue<PII,VII,greater<PII> > Q;
fill(d,d+*V,INF);
d[s]=;
Q.push(MP(d[s],s));
while(!Q.empty())
{
PII p=Q.top();Q.pop();
int v=p.Y;
if(d[v]<p.X)
continue;
REP(i,G[v].size())
{
PII e=G[v][i];
if(d[e.X]>d[v]+e.Y)
{
d[e.X]=d[v]+e.Y;
Q.push(MP(d[e.X],e.X));
}
}
}
} int main()
{_
int T,k=;
scanf("%d",&T);
while(T--)
{
CLR(G,);CLR(cnt,);
scanf("%d%d%d",&V,&M,&C);
REP(i,V)
{
int x;
scanf("%d",&x);
x--;
level[i]=x;
cnt[V+x]=;
}
REP2(i,V,*V-)
if(cnt[i] && cnt[i+])
if(i+<*V && cnt[i+])
addedge(i,i+,C);
REP(i,V)
{
G[V+level[i]].PB(MP(i,));
if(level[i]->=)
G[i].PB(MP(V+level[i]-,C));
if(level[i]+<V)
G[i].PB(MP(V+level[i]+,C));
}
REP(i,M)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
u--;v--;
addedge(u,v,w);
}
dijkstra();
printf("Case #%d: %d\n",k++,d[V-]==INF?-:d[V-]);
}
return ;
}
hdu4725 The Shortest Path in Nya Graph【最短路+建图】的更多相关文章
- HDU-4725 The Shortest Path in Nya Graph 最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...
- 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 题意: 在一个图中跑最 ...
- 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 ...
- HDU4725 The Shortest Path in Nya Graph SPFA最短路
典型的最短路问题,但是多了一个条件,就是每个点属于一个layer,相邻的layer移动,如x层移到x+1层需要花费c. 一种显而易见的转化是我把这些边都建出来,但是最后可能会使得边变成O(n^2); ...
- HDU4725 The Shortest Path in Nya Graph dij
分析:对于每一层,原来n个点,然后扩展为原来的三倍,每一层扩展一个入点,一个出点,然后跑最短路 注:tmd我把一个n写成m了,然后wa了7次,我都要怀疑人生了 #include<cstdio&g ...
- hdu4725 The Shortest Path in Nya Graph
这道题看了下很多人都是把每一层拆成两个点然后建图做的. 我的思路很直接,也不用建图,直接在更新每个点时更新他相邻的边和相邻的层,当然前提是每个点只更新一次,每个层也只更新一次,这样才能确保时间复杂度. ...
- 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)
职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...
- 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 ...
- 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 ...
随机推荐
- Listview 多个ViewHolder实现
简单代码示例: package com.edaixi.adapter; import android.content.Context; import android.view.View; import ...
- Tomcat与Web服务器、应用服务器的关系
Tomcat服务器是一个免费的开放源代码的Web应用服务器.因为Tomcat技术先进.性能稳定且免费,所以深受Java爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web应用服务器. A ...
- web编程速度大比拼(nodejs go python)(非专业对比)
C10K问题的解决,涌现出一大批新框架,或者新语言,那么问题来了:到底谁最快呢?非专业程序猿来个非专业对比. 比较程序:输出Hello World! 测试程序:siege –c 100 –r 100 ...
- 三种客户端访问wcf服务端的方法 C#
原文 http://blog.csdn.net/zlj002/article/details/7914556 string jsonstr = String.Empty; string url = & ...
- 类型萃取(type traits)
1. 类型萃取的作用 类型萃取使用模板技术来萃取类型(包含自定义类型和内置类型)的某些特性,用以判断该类型是否含有某些特性,从而在泛型算法中来对该类型进行特殊的处理用来提高效率或者其他.例如:在STL ...
- nfs error
mount -t nfs 10.173.55.154:/oradata /oradatamount: wrong fs type, bad option, bad superblock on 10.1 ...
- Quick Tip: Outline Elements on Hover
How to get the xpath by clicking an html element How to get the xpath by clicking an html element Qu ...
- CodeForces 452C Magic Trick (排列组合)
#include <iostream> #include <cstdio> #include<cmath> #include<algorithm> us ...
- SqlCacheDependency的使用
最近项目需要几秒就获取一次数据,查数据库服务器压力会很大,因此用缓存技术来缓解服务器压力. 使用SqlCacheDependency采用轮询的方式来获取缓存,SqlDependency查询通知的方式来 ...
- Objective-c 中的变量
OC中的语言变量,按作用域可分为两种:局部变量和全局变量. 局部变量:也称为内部变量,局部变量是在方法内部声明的.其作用域仅限于方法内,离开该方法再使用这个变量就是非法的. 全局变量:也称为外部变量, ...