HDU 4725 建图
http://acm.hdu.edu.cn/showproblem.php?pid=4725
The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7869 Accepted Submission(s): 1766
is a very easy problem, your task is just calculate el camino mas corto
en un grafico, and just solo hay que cambiar un poco el algoritmo. If
you do not understand a word of this paragraph, just move on.
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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
inline int read()
{
int r=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {r=r*+c-'';c=getchar();}
return r;
}
struct Edge
{
int to,w,next;
}e[];
int cnt,first[];
void add(int u,int v,int w)
{
e[cnt].to=v;
e[cnt].w=w;
e[cnt].next=first[u];
first[u]=cnt++;
}
struct node
{
int u,w;
bool operator<(const node &x)const{
return w>x.w;
}
};
int d[];
int dij(int N)
{
bool vis[];
priority_queue<node>Q;
memset(vis,,sizeof(vis));
memset(d,inf,sizeof(d));
d[]=;
Q.push(node{,});
while(!Q.empty()){
node t1=Q.top();Q.pop();
int u=t1.u;
if(vis[u]) continue;
vis[u]=;
if(u==N) break;
for(int i=first[u];i+;i=e[i].next){
Edge x=e[i];
if(!vis[x.to]&&d[x.to]>d[u]+x.w){
d[x.to]=d[u]+x.w;
Q.push(node{x.to,d[x.to]});
}
}
}
return d[N]==inf?-:d[N];
}
int spfa(int N)
{
bool vis[];
queue<int>Q;
memset(vis,,sizeof(vis));
memset(d,inf,sizeof(d));
Q.push();
vis[]=;
d[]=;
while(!Q.empty()){
int u=Q.front(); Q.pop();
vis[u]=;
for(int i=first[u];i+;i=e[i].next){
Edge x=e[i];
if(d[x.to]>d[u]+x.w){
d[x.to]=d[u]+x.w;
if(!vis[x.to]){
Q.push(x.to);
}
}
}
}
return d[N]==inf?-:d[N];
}
int main()
{
//ios::sync_with_stdio(false);
int T,N,M,C,i,j,k=;
int u,v,w,x[];
bool layer[];
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
T=read();
for(int cas=;cas<=T;++cas)
{
cnt=;
memset(first,-,sizeof(first));
memset(layer,,sizeof(layer));
cin>>N>>M>>C;
for(i=;i<=N;++i)
{
x[i]=read();
layer[x[i]]=;
}
for(i=;i<=N;++i)
{
add(x[i]+N,i,);
add(i,x[i]+N,);
if(x[i]>&&layer[x[i]-])
add(i,x[i]+N-,C);
if(x[i]<N&&layer[x[i]+])
add(i,x[i]+N+,C);
}
while(M--){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
printf("Case #%d: %d\n",cas,dij(N)/*spfa(N)*/);
}
return ;
}
HDU 4725 建图的更多相关文章
- hdu 2768(建图,最大点独立集)
Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...
- HDU 4725 The Shortest Path in Nya Graph( 建图 + 最短路 )
主要是建图,建好图之后跑一边dijkstra即可. 一共3N个点,1~N是原图中的点1~N,然后把每层x拆成两个点(N+x)[用于连指向x层的边]和(N+N+x)[用于连从x层指出的边]. 相邻层节点 ...
- 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 4444 Walk (离散化+建图+bfs+三维判重 好题)
Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- Food HDU - 4292 网络流 拆点建图
http://acm.hdu.edu.cn/showproblem.php?pid=4292 给一些人想要的食物和饮料,和你拥有的数量,问最多多少人可以同时获得一份食物和一份饮料 写的时候一共用了2种 ...
- hdu 2647 (拓扑排序 邻接表建图的模板) Reward
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...
- HDU 4370 0 or 1(spfa+思维建图+计算最小环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 题目大意:有一个n*n的矩阵Cij(1<=i,j<=n),要找到矩阵Xij(i< ...
- HDU 3639 Hawk-and-Chicken(强连通缩点+反向建图)
http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...
随机推荐
- 我的Android进阶之旅------>关于使用Android Studio替换App的launcher图标之后仍然显示默认的ic_launcher图标的解决方法
前言 最近做了一个App,之前开发该App的时候一直以来都是默认的launcher图标启动的, 今天美工换了一个App的launcher 图标,因此在Android Studio中将默认的lanche ...
- Java排序算法总结(转载)
排序算法 平均时间复杂度 冒泡排序 O(n2) 选择排序 O(n2) 插入排序 O(n2) 希尔排序 O(n1.5) 快速排序 O(N*logN) 归并排序 O(N*logN) 堆排序 O(N*log ...
- keras中 LSTM 的 [samples, time_steps, features] 最终解释
I am going through the following blog on LSTM neural network:http://machinelearningmastery.com/under ...
- 线性表 - C语言完整实现
#include <stdio.h> #define false 0 #define true 1 #define MAXSIZE 20 typedef int bool; typedef ...
- yum速查
yum命令是在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包, 能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖 ...
- s5_day5作业
# 1.写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成批量修改操作 # def number_file(file,change_s,change): # import os # with ...
- 使用curl 命令模拟POST/GET请求
https://blog.csdn.net/u012340794/article/details/71440604 curl命令是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载 ...
- docker安装部署PHP nginx
sudo tee /etc/yum.repos.d/docker.repo <<-'EOF' [dockerrepo] name=Docker Repository baseurl=htt ...
- jmeter常用插件安装
转载:http://www.cnblogs.com/danqiu/p/6119156.html 下载地址:http://jmeter-plugins.org/downloads/all/ PerfMo ...
- 20145217《信网络对抗》逆向与BOF基础实践
20145217<信网络对抗>逆向与BOF基础实践 内容: 一.简单机器指令,汇编语言 1.'objdump -d xxx|more'反汇编命令查看机器代码,'cat'显示文件内容,'xx ...