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)
Problem Description
This 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.
Input
The first line has a number T (T <= 20) , indicating the number of test cases.
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.
Output
For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
If there are no solutions, output -1.
Sample Input
2
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
Sample Output
Case #1: 2
Case #2: 3
题意:有n个点,m条通路,每个点到相邻的层需要w权值,告诉每个点所在的层数,每条通路连接的点及权值,1到n点的最小权值。
题解:最短路问题,因为数据量问题所以用dijstral算法+优先队列。
- 难点在于如何解决每个点到相邻的通路解决问题,注意如果这一层没有点是不用考虑的。
我在每一个点加了一个对应的虚拟点,其他相邻的层的点到虚拟点的距离为w,虚拟点到这个点的距离为0,这样就实现了相邻层到达的问题。
另外这个题目数组大小问题,因为加了点的问题,建议点的数目是题目给的两倍以上,路的数目是题目给的五倍以上。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
const int INF = 1e9+7;
const int maxn = 500050;
struct node
{
int u,w;
node(int a,int b):u(a),w(b){}
bool operator <(const node &q)const
{
return w>q.w;
}
};
struct edge
{
int to,w,next;
}s[maxn];
int num,head[maxn],m,n;
int f[maxn],a[maxn],dis[maxn];
void add(int u,int v,int w)
{
s[num].to = v;
s[num].w = w;
s[num].next = head[u];
head[u] = num++;
}
void dij()
{
priority_queue<node> q;
memset(f,0,sizeof(f));
int i,u,v,w;
for(i=0;i<=2*n;i++)
dis[i] = INF;
dis[1] = 0;
q.push(node(1,0));
while(!q.empty())
{
node t1 = q.top();
q.pop();
u = t1.u;
if(f[u])
continue;
f[u] = 1;
for(i=head[u];i!=-1;i=s[i].next)
{
v = s[i].to;
w = s[i].w;
if(f[v])
continue;
if(dis[u]+w<dis[v])
{
dis[v] = dis[u] + w;
q.push(node(v,dis[v]));
}
}
}
if(dis[n]==INF)
printf("-1\n");
else
printf("%d\n",dis[n]);
}
int main()
{
int t,i,k=1,u,v,w,x;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&x);
memset(head,-1,sizeof(head));
memset(f,0,sizeof(f));
num = 0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
f[a[i]] = 1;
}
for(i=1;i<=n;i++)
{
if(a[i]==1)/*如果是第一层的话只要链接上一层就可以了*/
{
add(a[i]+n,i,0);
if(f[a[i]+1]&&a[i]<n)
add(i,a[i]+n+1,x);
}
else if(a[i]==n)/*如果是第n层的话只要链接下一层就可以了*/
{
add(a[i]+n,i,0);
if(f[a[i]-1]&&a[i]>1)
add(i,a[i]+n-1,x);
}
else
{
add(a[i]+n,i,0);
if(f[a[i]-1]&&a[i]>1)
add(i,a[i]+n-1,x);
if(f[a[i]+1]&&a[i]<n)
add(i,a[i]+n+1,x);
}
}
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
printf("Case #%d: ",k++);
dij();
// for(i=head[1];i!=-1;i=s[i].next)
// {
// printf("%d %d\n",s[i].to,s[i].w);
// }
}
return 0;
}
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 (最短路+建图)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU4725: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 (拆点+dji)
HDU 4725 The Shortest Path in Nya Graph : http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意: 在一个图中跑最 ...
- (中等) 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 ...
随机推荐
- css之页面三列布局之左右两边宽度固定,中间自适应
左右两边宽度固定,中间自适应 左右两边绝对定位 可以利用浮动,左边的左浮动,右边的右浮动 css3 flex布局(html http://www.cnblogs.com/myzy/p/5919814. ...
- vue开发中控制台报错问题
1.sockjs.js?9be2:1606 GET http://localhost:8566/sockjs-node/info?t=1569478261510 net::ERR_CONNECTION ...
- 从大数据到快数据 数据智创未来——2019 CCF大数据与计算智能大赛正式开赛!
8月17日,以“数据驱动,智创未来”为主题的2019 CCF大数据与计算智能大赛(CCF Computing Intelligence Contest,简称CCF BDCI)全球启动仪式,在北京大学正 ...
- 洛谷P3306 随机数生成器
题意:给你一个数列,a1 = x,ai = (A * ai-1 + B) % P,求第一个是t的是哪一项,或者永远不会有t. 解:循环节不会超过P.我们使用BSGS的思想,预处理从t开始跳√P步的,插 ...
- Java中字符串为什么不以\0结尾
Java中字符串为什么不以\0结尾 其实这个问题没有什么好说的,Java里面一切都是对象,是对象的话,字符串肯定就有长度,即然有长度,编译器就可以确定要输出的字符个数,当然也就没有必要去浪费那1字节的 ...
- Java基础-注解
什么是注解? Jdk1.5新增新技术,注解.很多框架为了简化代码,都会提供有些注解.可以理解为插件,是代码级别的插件,在类的方法上写:@XXX,就是在代码上插入了一个插件. 注解不会也不能影响代码的实 ...
- hbase phoenix char may not be null
在使用phoenix做hbase的相关測试的时候.会出现 char may not be null 的错误. 这是因为建表和导入的数据不匹配导致的.主要是char的定义,假如一个字段定义为char类型 ...
- day38 07-Spring框架Bean的时候方式
Spring是自动帮我们创建对象的,有几种创建Bean的方式呢? 构造方法实例化:(默认无参数)其实就是反射new Instance(). 静态工厂实例化: 实例工厂实例化: 一般不会改变它实例化的方 ...
- sar-CPU统计数据
sar -u输出显示CPU信息.-u选项是sar的默认选项.该输出以百分比显示CPU的使用情况.表3-2解释该输出. 表3-2 ...
- [翻译] MaxMind DB 文件格式规范
MaxMind DB 文件格式规范来源:http://maxmind.github.io/MaxMind-DB/翻译:御风(TX:965551582)2017-03-23 -------------- ...