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 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...
随机推荐
- centos7 Docker Compose 的安装
[root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose ...
- Android 成功 使用GPS获取当前地理位置(解决getLastKnownLocation 返回 null)
最近遇到一个比较棘手的问题:使用GPS定位无法获取当前的地理位置,即getLastKnownLocation方法始终返回null. 后来一篇博文 getLastKnownLocation()返回n ...
- 吴超老师课程--HBASE的集群安装
1.hbase的机群搭建过程(在原来的hadoop上的hbase伪分布基础上进行搭建)1.1 集群结构,主节点(hmaster)是hadoop,从节点(region server)是hadoop1和h ...
- python16_day16【Django_ORM、模板】
一.ORM 1.MySQL配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'books', #你 ...
- Excel常见操作,重复数据,去除数据关联
Eecel对一个数据进行操作后按住右下角的十字架往下拉就可以对下面的操作进行相同 的操作,所以只需先对一个数据进行操作,再拉下来就可以了 通过公式处理的数据跟其它数据有关联 需要对这些数据进行去除它们 ...
- ruby项目文件上传功能实现
这里我将从视图.控制器各个层面进行讲解. rails 提供了文件上传功能,可以直接进行下面的编码 <%= form_for :document, :html =>{:multipart = ...
- 看github上有18万star的第一开源项目如何教你学前端编程的
作为 Github | star 第一开源项目,已经超过18万 star:比之前最火的bootstrap的10万star还要多出8w,freeCodeCamp 越来越受关注,建站两年时间不到已经近40 ...
- 笔记-CSS空背景图片会导致页面被加载两次
如果页面样式的背景图片路径设置为'' 或 '#', 会导致页面被重复加载两次 (Chrome.56.0.2924.87 测试) 因为:空图片路径属性值,默认加载当前页面的URL作为图片路径 Safar ...
- BZOJ 5312: 冒险
首先我们考虑,对于And 和 Or 操作,对于操作位上只有And 0 和 Or 1 是有效果的. 我们注意到如果区间内需要改动的操作位上的数字都相同,那么是可以区间取与以及区间取或的. 那其实可以维护 ...
- Unity,自带Random函数,上下限注意的地方
Random.Range() 该函数有两个重载,分别是 float和 int 的,这两者还是有差别的,具体是: float型,随机值涵盖: 最小和最大值 Random.Range(0f,1f) 是有可 ...