HDU 4725 The Shortest Path in Nya Graph (最短路 )
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.
For each test case, first line has three numbers N, M (0 <= N, M <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), 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.
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
Case #1: 2
Case #2: 3
分析:
本题的坑点在于建图的时候边数太多,因为每x层与x+1层中的每一个点都相互连接,这样就有 Nx^N(x+1) 条边,sum(Ni)为10^5,暴力建图肯定会爆的
建图的时候,每一层增加一个“层点”作为中介,然后层点与层点建边,层点与在该层上的点建边(边长为0),点与相邻层点建边(边长为c)。
最后增加的边,点与点建边。
在赛场上怂了,看见10^5没敢多建边,只想着如何在层之间建边了。。。对时间复杂度与时间限制的把握还是不好,以后要记住常用算法的复杂度,还要学习如何准确分析时间复杂度
代码:
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <queue>
5 #include <vector>
6 #define maxn 200005
7 #define INF 0x3f3f3f3f
8 using namespace std;
9
10 int n,m,c,ans,cnt;
11 bool vis[maxn],vv[maxn];
12 int dist[maxn],pp[maxn],lay[maxn];
13 struct Node
14 {
15 int v,w;
16 int next;
17 } edge[20*maxn];
18 queue<int>q;
19
20 void init()
21 {
22 memset(pp,0,sizeof(pp));
23 memset(vv,0,sizeof(vv));
24 memset(dist,0x3f,sizeof(dist));
25 }
26 void addedge(int u,int v,int w)
27 {
28 cnt++;
29 edge[cnt].v=v;
30 edge[cnt].w=w;
31 edge[cnt].next=pp[u];
32 pp[u]=cnt;
33 }
34 void SPFA()
35 {
36 int i,j,u,v,w;
37 memset(vis,0,sizeof(vis));
38 while(!q.empty()) q.pop();
39 dist[1]=0;
40 vis[1]=1;
41 q.push(1);
42 while(!q.empty())
43 {
44 u=q.front();
45 q.pop();
46 vis[u]=0;
47 for(i=pp[u]; i; i=edge[i].next)
48 {
49 v=edge[i].v;
50 w=edge[i].w;
51 if(dist[v]>dist[u]+w)
52 {
53 dist[v]=dist[u]+w;
54 if(!vis[v])
55 {
56 vis[v]=1;
57 q.push(v);
58 }
59 }
60 }
61 }
62 }
63 int main()
64 {
65 int i,j,t,u,v,w,test=0;
66 scanf("%d",&t);
67 while(t--)
68 {
69 scanf("%d%d%d",&n,&m,&c);
70 init();
71 cnt=0;
72 for(i=1; i<=n; i++)
73 {
74 scanf("%d",&u);
75 lay[i]=u;
76 vv[u]=1;
77 }
78 for(i=1; i<n; i++)
79 {
80 if(vv[i]&&vv[i+1]) // 两层都出现过点相邻层才建边
81 {
82 addedge(n+i,n+i+1,c);
83 addedge(n+i+1,n+i,c);
84 }
85 }
86 for(i=1; i<=n; i++) // 层到点建边 点到相邻层建边
87 {
88 addedge(n+lay[i],i,0);
89 if(lay[i]>1) addedge(i,n+lay[i]-1,c);
90 if(lay[i]<n) addedge(i,n+lay[i]+1,c);
91 }
92 for(i=1; i<=m; i++) // 点到点建边
93 {
94 scanf("%d%d%d",&u,&v,&w);
95 addedge(u,v,w);
96 addedge(v,u,w);
97 }
98 SPFA();
99 printf("Case #%d: ",++test);
100 ans=dist[n];
101 if(ans<INF) printf("%d\n",ans);
102 else printf("-1\n");
103 }
104 return 0;
105 }
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 ...
- (中等) 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 ...
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- HDU - 4725 The Shortest Path in Nya Graph 【拆点 + dijkstra】
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
随机推荐
- codeforces 816B Karen and Coffee (差分思想)
题目链接 816B Karen and Coffee 题目分析 题意:有个人在学泡咖啡,因此看了很多关于泡咖啡温度的书,得到了n种推荐的泡咖啡温度范围[L1,R1] ,此人将有k种做法推荐的温度记为可 ...
- JavaScript使用readAsDataURL读取图像文件
JavaScript使用readAsDataURL读取图像文件 FileReader对象的readAsDataURL方法可以将读取到的文件编码成Data URL.Data URL是一项特殊的技术,可以 ...
- phpmyadmin出现空密码登录被禁止 (参见 允许空密码)的解决办法
在Windows或者Linux下mysql安装后默认的密码为空,又当我们又安装了mysql的管理工具phpmyadmin后登陆时出现“空密码登陆呗禁止(参见允许密码为空)”.不能登录成功 ...
- CF 403D Beautiful Pairs of Numbers
The sequence of integer pairs (a1, b1), (a2, b2), ..., (ak, bk) is beautiful, if the following state ...
- java程序中访问https时,报 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
在java中使用https访问数据时报异常: Caused by: sun.security.validator.ValidatorException: PKIX path building fail ...
- SSM和Spring Boot常用配置比较
一.Dao层相关 1.Mysql相关: 1.1配置DataSource连接池: (1)SSM配置: <!-- 加密后配置自己写的解析文件 --> <bean class=" ...
- spring boot 开启Druid监控功能
1.配置yml spring: datasource: # 数据源基本配置 username: song password: 123456 driver-class-name: com.mysql.j ...
- 全自动链接克隆KVM虚拟机
virt-clone这个命令是基于全克隆的,也就是拷贝虚拟磁盘文件和虚拟配置文件来实现的完整克隆,速度慢,占用空间多 kvm软件包中并没有实现全自动链接克隆的命令或工具,只能手动实现,于是我决定写一个 ...
- 为Qtcreator 编译的程序添加管理员权限
(1)创建资源文件 myapp.rc 1 24 uac.manifest (2)创建文件uac.manifest <?xml version="1.0" encoding=& ...
- springmvc框架中的核心对象DispatcherServlet
SpringMVC是Spring中的模块,它实现了mvc设计模式,首先用户发起请求,请求到达SpringMVC的前端控制器(DispatcherServlet),前端控制器根据用户的url请求处理器映 ...