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 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...
随机推荐
- SpringMVC是单例的,高并发情况下,如何保证性能的?
首先在大家的思考中,肯定有影响的,你想想,单例顾名思义:一个个排队过... 高访问量的时候,你能想象服务器的压力了... 而且用户体验也不怎么好,等待太久~ 实质上这种理解是错误的,Java里有个A ...
- 转!!xss漏洞
参考资料 https://blog.csdn.net/jiangzhexi/article/details/56841793 http://www.freebuf.com/articles/web/4 ...
- Keras网络层之卷积层
卷积层 Cov1D层 keras.layers.convolutional.Conv1D(filters, kernel_size, strides=1, padding='valid', dilat ...
- 简明python教程七----面向对象的编程
根据操作数据的函数或语句块来设计程序的,被称为面向过程的编程. 把数据和功能结合起来,用称为对象的东西包裹起来的组织程序的方法,称为面向对象的编程理念. 类和对象是面向对象编程的两个主要方面.类创建一 ...
- HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 刚开始没考虑时间复杂度,直接敲了,直接tle了,之后没有思路,然后看题解,看见大神写的优化非常棒. ...
- Django-进阶之路--信号
Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行 ...
- HDU - 6311 Cover (欧拉路径)
题意:有最少用多少条边不重复的路径可以覆盖一个张无向图. 分析:对于一个连通块(单个点除外),如果奇度数点个数为 k,那么至少需要max{k/2,1} 条路径.将奇度数的点两两相连边(虚边),然后先 ...
- Firebug入门指南(转)
本文转自:http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html 作者: 阮一峰 日期: 2008年6月 8日 据说,对于网页开发人员 ...
- Java基础_基本语法
Java基本语法 一:关键字 在Java中有特殊含义的单词(50). 二:标志符 类名,函数名,变量名的名字的统称. 命名规则: 可以是字母,数字,下划线,$. 不能以数字开头. 见名之意. 驼峰规则 ...
- LeetCode——Insertion Sort List
LeetCode--Insertion Sort List Question Sort a linked list using insertion sort. Solution 我的解法,假设第一个节 ...