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 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...
随机推荐
- Windows Server 2016 下执行定时任务(英文系统)
Step1. 找到“控制面板(Control Panel)” 打开 “管理工具(Administrative Tools)” Step2. 打开“任务计划(Task Schedule)” Step3. ...
- 链路的有效性检测 及 基于TCP的通信为什么需要RETRY
一.链路的有效性检测 当网络发生单通.连接被防火墙Hang住.长时间GC或者通信线程发生非预期异常时,会导致链路不可用且不易被及时发现. 特别是异常发生在凌晨业务低谷期间,当早晨业务高峰期到来时,由于 ...
- 查看Oracle的表中有哪些索引
用user_indexes和user_ind_columns系统表查看已经存在的索引 对于系统中已经存在的索引我们可以通过以下的两个系统视图(user_indexes和user_ind_columns ...
- 011-JDK可视化监控工具-Jstat
一.概述 Jstat 是JDK自带的一个轻量级小工具.全称“Java Virtual Machine statistics monitoring tool”,它位于java的bin目录下,主要利用JV ...
- webdriver的API
基本API 1.页面刷新 driver.fresh() 2.页面切换 driver.back(). driver.forward() 3.设置窗口大小 driver.set_wind ...
- mysql安装前的系统准备工作
一.系统环境总结:
- 安装vue-cli脚手架
一.安装node.js 1.什么是node.js? Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模 ...
- 主机名 域名 网站名 URL
举几个域名的例子:google.com,baidu.com,163.com可以明确的告诉你,加上www,就不再是域名了! 以http://mail.163.com/index.html为例进行说明:1 ...
- 解锁CHM文件
刚在网上下载了CHM格式的文件,打开之后,右侧部分一片空白. 原因:可能是,系统针对来源不明文件的一种保存措施吧 解决方法: 1.选中文件,右键打开属性对话框,在 “常规”选项卡中 勾选 [解除锁定] ...
- POJ - 2762 Going from u to v or from v to u? (强连通缩点+判断单向连通)
题意:判断一个有向图中的任意两点u.v,是否可以由其中一个点到达另一个点. 分析:这个问题转化以后就是:将该图强连通缩点后再判断其是否是单向连通的.缩点用Tarjan处理强连通分量. 有一个定理是这样 ...