(中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description
The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total.
You can move from any node in layer x to any node in layer x +
1, with cost C, since the roads are bi-directional, moving from layer x
+ 1 to layer x is also allowed with the same cost.
Besides, there are M extra edges, each connecting a pair of node u and v, with cost w.
Help us calculate the shortest path from node 1 to node N.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=*;
const int MaxM=*;
const int INF=10e9+; struct Edge
{
int to,next,cost;
}; struct Node
{
int v,val; Node(int _v=,int _val=):v(_v),val(_val) {} bool operator < (const Node &a) const
{
return val>a.val;
}
}; Edge E[MaxM];
int head[MaxN],Ecou; void init(int N)
{
Ecou=; for(int i=;i<=N;++i)
head[i]=-;
} void addEdge(int u,int v,int c)
{
E[Ecou].to=v;
E[Ecou].cost=c;
E[Ecou].next=head[u];
head[u]=Ecou++;
} bool vis[MaxN]; void Dijkstra(int lowcost[],int N,int start)
{
priority_queue <Node> que;
Node temp;
int u,v,c;
int len; for(int i=;i<=N;++i)
{
lowcost[i]=INF;
vis[i]=;
} lowcost[start]=;
que.push(Node(start,)); while(!que.empty())
{
temp=que.top();
que.pop(); u=temp.v; if(vis[u])
continue; vis[u]=; for(int i=head[u];i!=-;i=E[i].next)
{
v=E[i].to;
c=E[i].cost; if(!vis[v] && lowcost[v]> lowcost[u]+c)
{
lowcost[v]=lowcost[u]+c;
que.push(Node(v,lowcost[v]));
}
}
}
} int ans[MaxN];
bool have[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T;
int N,M,C;
int u,v,c;
int cas=; scanf("%d",&T); while(T--)
{ scanf("%d %d %d",&N,&M,&C); init(*N);
memset(have,,sizeof(have)); for(int i=;i<=N;++i)
{
scanf("%d",&u); addEdge(i,N+*u-,);
addEdge(N+*u,i,); have[u]=;
} for(int i=;i<N;++i)
if(have[i] && have[i+])
{
addEdge(N+*i-,N+*(i+),C);
addEdge(N+*(i+)-,N+*i,C);
} for(int i=;i<=M;++i)
{
scanf("%d %d %d",&u,&v,&c); addEdge(u,v,c);
addEdge(v,u,c);
} Dijkstra(ans,*N,); printf("Case #%d: %d\n",cas++,ans[N]==INF ? - : ans[N]);
} return ;
}
(中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。的更多相关文章
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- FirstOrDefault()的重载方法
FirstOrDefault方法的使用总结: 现有一集合对象list, 其中集合对象调用FirstOrDefault()方法, list.FirstOrDefault()返回集合中第一个元素, 若集合 ...
- 解决centos无法上传文件和打开文件夹
使用yum搭建了ftp服务..yum的使用参考:http://blog.csdn.net/enson16855/article/details/9140623 windows使用FileZilla连接 ...
- VS2010+PCL+openni配置
PCL中文论坛:http://www.pclcn.org/bbs/forum.php 1.安装 pcl 的完全安装包可以到: http://pointclouds.org/downloads/wind ...
- Tinyxml封装类COperatorXml
OperatorXml.h头文件 #ifndef _OPERATOR_XML_H_ #define _OPERATOR_XML_H_ #include <string> class TiX ...
- druid 文档 和 源码地址
Druid文档 :https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98 maven仓库:http://c ...
- 使用Qt报错error while building deploying project
方法一:点击左侧的“项目”栏,看“构建目录”栏的路径,一定要注意,在路径中一定不要出现汉字,否则一定会报“error while building deploying project”的错误. 方法二 ...
- IE8浏览器兼容问题总结
IE8+兼容经验小结 January 15, 2014 本文分享下我在项目中积累的IE8+兼容性问题的解决方法.根据我的实践经验,如果你在写HTML/CSS时候是按照W3C推荐的方式写的,然后下面的几 ...
- Windows API 之 GetStartupInfo 、CreateProcess
GetStartupInfo 参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms683230%28v=vs.85%29.asp ...
- linux常用命令 、查看日志、web排查
linux常用命令 ps aux|grep xxx (比如 ps aux|grep tomcat ps aux|grep tomcat-portalvip ps aux|grep nginx 等) r ...
- PHP个人常用函数封装
function GetIP(){ if(!empty($_SERVER["HTTP_CLIENT_IP"])){ $cip = $_SERVER["HTTP_CLIEN ...