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 ...
随机推荐
- Oracle游标
转自:http://www.cnblogs.com/fjfzhkb/archive/2007/09/12/891031.html 游标-----内存中的一块区域,存放的是select 的结果 ...
- codinglife主题小修改和有意思的博客挂件
这个主题很漂亮,不过为了迎合自己的喜好ヽ(•̀ω•́ )ゝ,修改了字号.阴影之类的小细节.同时下面还有我博客里面的两个有意思的小挂件,请向右边看(๑و•̀ω•́)و 1.主题修改:复制下面的css代码 ...
- 你需要了解的JS框架
excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js 用途:构建数据统计图表,兼容多浏览器 ...
- 有一个警告:Could not open/create prefs root node
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. 虽然程序也能正常运 ...
- latex 常用小结
在写论文,甚至有些课程的报告的时候,latex是常用的工具.这篇博文简单的记录了latex常用的一些内容. 1 基本模块 没用过latex的读者,最想问的问题莫过于latex的 “hello worl ...
- Windows 键盘快捷键
Windows 键盘快捷键 标签页和窗口快捷键 Ctrl+N 打开新窗口. Ctrl+T 打开新标签页. Ctrl+Shift+N 在隐身模式下打开新窗口. 按 Ctrl+O,然后选择文件. 通过 G ...
- Delphi2010新发现-类的构造和析构函数功能
Delphi2010发布了. 虽然凭着对Delphi的热爱第一时间就安装了,但是现在可能是年纪大了,对新事物缺乏兴趣了.一直都没有仔细研究. 今天有点时间试了一下新功能. 本来C#和Delphi.NE ...
- css包含块containing block
<css权威指南>P167: The Containing Block Every element is laid out with respect to its containing b ...
- 收藏:左路Deep Learning+右路Knowledge Graph,谷歌引爆大数据
发表于2013-01-18 11:35| 8827次阅读| 来源sina微博 条评论| 作者邓侃 数据分析智能算法机器学习大数据Google 摘要:文章来自邓侃的博客.数据革命迫在眉睫. 各大公司重兵 ...
- DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建
这个系列通过一些简单例子循序渐进,介绍DevExpress ASP.NET控件的使用.先来介绍一下XPO的使用,安装的DevExpress版本为DXperienceUniversal-12.2.4,使 ...