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 (Java/Others)
Total Submission(s): 11694 Accepted Submission(s): 2537
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.
For each test case, first line has three numbers N, M (0 <= N, M <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), which means there is an extra edge, connecting a pair of node u and v, with cost w.
If there are no solutions, output -1.
3 3 3
1 3 2
1 2 1
2 3 1
1 3 3
3 3 3
1 3 2
1 2 2
2 3 2
1 3 4
Case #2: 3
//spfa用队列实现一般较快
//主要是一个建图的构思(是不是学完网络流会领会更多呢) #include <stack>
#include <queue>
#include <cstdio>
#include <cstring> using namespace std; const int maxn = ; int layer[maxn+]; struct
{
int to;
int w;
int next;
}edge[maxn*+];
//点的编号1..n 每层再安排一个出点n+1..n*2 一个入点n*2+1..n*3
int head[maxn*+];
int vis[maxn*+];
int dis[maxn*+]; int main()
{
int t,k=;
scanf("%d",&t);
while(t--)
{
int n,m,c;
scanf("%d%d%d",&n,&m,&c);
for(int i=;i<=n;i++)
scanf("%d",layer+i); //建边
memset(head,-,sizeof(head));
int cnt=;
for(int i=;i<m;i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
edge[cnt].to=b;edge[cnt].w=w;edge[cnt].next=head[a];head[a]=cnt++;
edge[cnt].to=a;edge[cnt].w=w;edge[cnt].next=head[b];head[b]=cnt++;
}
//每层安排一个入点一个出点
for(int i=;i<=n;i++)
{
int a,b,w;
a=i;b=n+layer[i];w=;
edge[cnt].to=b;edge[cnt].w=w;edge[cnt].next=head[a];head[a]=cnt++;
a=n*+layer[i];b=i;w=;
edge[cnt].to=b;edge[cnt].w=w;edge[cnt].next=head[a];head[a]=cnt++;
}
//额外安排的点相邻是C为代价的
for(int i=;i<n;i++)
{
int a,b,w;
a=n+i;b=n*+i+;w=c;
edge[cnt].to=b;edge[cnt].w=w;edge[cnt].next=head[a];head[a]=cnt++;
a=n+i+;b=n*+i;w=c;
edge[cnt].to=b;edge[cnt].w=w;edge[cnt].next=head[a];head[a]=cnt++;
}
//printf("11111\n"); //spfa开跑!
memset(vis,,sizeof(vis));
memset(dis,-,sizeof(dis));
queue<int> s;
s.push();
vis[]=;
dis[]=;
while(!s.empty())
{
int p=s.front();s.pop();
vis[p]=;
//printf("%d\n",p);
for(int i=head[p];i!=-;i=edge[i].next)
{
int v=edge[i].to,w=edge[i].w;
if(dis[v]==-||dis[v]>dis[p]+w)
{
dis[v]=dis[p]+w;
if(!vis[v])
s.push(v),vis[v]=;
}
}
}
//printf("22222\n"); printf("Case #%d: %d\n",k++,dis[n]);
}
return ;
}
hdu 4725 The Shortest Path in Nya Graph (最短路+建图)的更多相关文章
- 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,Dijkstra+加点。
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(最短路径)(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 ...
随机推荐
- IE6下CSS常见兼容性问题及解决方案
1. 在IE6元素浮动,如果宽度需要内容撑开,就给里面的块元素加浮动. 2. IE6下最小高度问题:在IE6下元素高度小于19px的时候,会被当作19px处理.解决方案:给元素加 overflow:h ...
- useReducer代替Redux
创建state.js import React, { createContext,useContext,useReducer } from 'react'; export const countTex ...
- PostGIS 爆管分析之找出上游阀门
环境: Win10 ArcMap10.4(用于数据处理) postgresql9.4 postgis2.2.3 pgRouting2.3(postgresql插件) 说明: 继上一篇文章做了爆管分析找 ...
- day 28 网络基础相关的知识
1.网络基础相关的知识 架构 C/S 架构: client 客户端 server服务器端 优势: 能充分发挥PC机的性能 B/S 架构: browser 浏览器 server服务器 隶 ...
- c# 基于DataTable的Compute方法的扩展
DataTable.Compute(String, String) 方法 定义 命名空间:System.Data 程序集:System.Data.dll, netstandard.dll, Syste ...
- html代码/如何做到有横线无竖线的表格/或横线有颜色/竖线没颜色
改变它的css样式,table{ border-collapse:collapse;}table tr td{ border-bottom:1px solid #dedede;}
- C语言l-2019秋作业01
2.1 你对软件工程专业或者计算机科学与技术专业了解是怎样? 在进入大学之前,我认为软件工程就是学习开发软件的,后来,从网上搜索了有关它的定义,软件工程是一门研究用工程化方法构建和维护软件的学科,它以 ...
- Spring Boot中使用Jpa的findOne方法不能传入id
最近通过慕课网学习spring boot,视频中通过jpa的findOne方法以id为参数查询出对应的信息, 而当我自己做测试的时候却发现我的findOne方法的参数没有Integer类型的id,而是 ...
- 【Python成长之路】从零学GUI -- 制作智能聊天机器人
[写在前面] 鹏哥:最近老惹小燕同学不开心,结果都没人陪我聊天了.哎,好无聊呀! 肥宅男:女朋友什么的最无聊了,还没我的图灵机器人好玩. 鹏哥:图灵?好巧,和我部门同名. [效果如下] [实现过程] ...
- Android 自定义 View 详解
View 的绘制系列文章: Android View 绘制流程之 DecorView 与 ViewRootImpl Android View 的绘制流程之 Measure 过程详解 (一) Andro ...