P - The Shortest Path in Nya Graph HDU - 4725

最短路
不是 每两个点之间按层数设置边权 + 额外边权 TLE
是 相邻两层之间设置边权 + 额外边权

需注意 add 添加得是点的关系而不是层得关系

以下加边得方法 TLE 了,应该是因为太多重复加边, 如 2 1-2 2-1 ; 1 1-2

     map<int, int> mp;scanf("%d%d%d", &N, &M, &C);for(int i = 1; i <= N; i++) scanf("%d", &num[i]), mp[num[i]] = i;for(int i = 1; i <= N; i++) {if(num[i] == 1) add(i, mp[2], C), add(mp[2], i, C);else if(num[i] == N) add(i, mp[N-1], C), add(mp[N-1], i, C);else add(i, mp[num[i]+1], C), add(mp[num[i]+1], i, C), add(mp[num[i]-1], i, C), add(i, mp[num[i]-1], C);}for(int i = 1; i <= M; i++) {scanf("%d%d%d", &u, &v, &w);add(u, v, w); add(v, u, w);}

由于点的个数太多,如果对每个相邻层之间的每个点都建边,当只有两层时,DJ最坏复杂度为 O(n^2)
所以不能对层与层之间的所有点之间建边,可以在每一层找一个出点,一个入点,层内每个点都与出入点建边,边权为0, 通过出入点与外界联系

建边如下

     for(int i = 1; i <= N; i++) {scanf("%d", &x);lay[x] = 1; // x 层中有点add(i, N + 2*x, 0); // num[i] 层的出点 add(N + 2*x - 1, i, 0);  // num[i] 层的入点 }for(int i = 1; i <= N; i++) {if(lay[i] && lay[i+1]) {add(N+i*2, N+(i+1)*2-1, C); add(N+(i+1)*2, N+i*2-1, C); // 相邻层之间由出入点相连 }}for(int i = 1; i <= M; i++) {scanf("%d%d%d", &u, &v, &w);add(u, v, w); add(v, u, w);}

AC代码

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
const int maxn = 3e5+10;
const int inf = 1e9+7;
int N, M, C;
int lay[maxn];
int head[maxn], cnt, dis[maxn];
bool vis[maxn];
struct edge {int to, next, w;
}e[maxn*2];void add(int u, int v, int w) {e[++cnt].next = head[u];e[cnt].to = v;e[cnt].w = w;head[u] = cnt;
}typedef struct node {int u, dist;
} cam;bool operator < (const node &a, const  node &b) {return a.dist > b.dist;
}void dijkstra() {for(int i = 0; i <= N*3; i++) dis[i] = inf; // 因为点的个数有 3*N = N + 2*N cam s; s.dist = 0; s.u = 1;dis[1] = 0;priority_queue<cam> heap;heap.push(s);while(heap.size()) {cam now = heap.top(); heap.pop();if(vis[now.u]) continue;vis[now.u] = 1;for(int i = head[now.u]; i; i = e[i].next) {int to = e[i].to;if(dis[to] > now.dist + e[i].w) {dis[to] = now.dist + e[i].w;cam tmp; tmp.dist = dis[to]; tmp.u = to;heap.push(tmp);}}}
}void init() {memset(head, 0, sizeof(head));cnt = 0;memset(vis, 0, sizeof(vis));memset(lay, 0, sizeof(lay));
}int main() {//  freopen("test.in", "r", stdin);int T; scanf("%d", &T);int kase = 0;while(T--) {init();int u, v, w, x;scanf("%d%d%d", &N, &M, &C);for(int i = 1; i <= N; i++) {scanf("%d", &x);lay[x] = 1; // x 层中有点add(i, N + 2*x, 0); // num[i] 层的出点 add(N + 2*x - 1, i, 0);  // num[i] 层的入点 }for(int i = 1; i <= N; i++) {if(lay[i] && lay[i+1]) {add(N+i*2, N+(i+1)*2-1, C); add(N+(i+1)*2, N+i*2-1, C); // 相邻层之间由出入点相连 }}for(int i = 1; i <= M; i++) {scanf("%d%d%d", &u, &v, &w);add(u, v, w); add(v, u, w);}dijkstra();
//      for(int i = 1; i <= N; i++) cout << dis[i] << endl;if(dis[N] == inf) printf("Case #%d: -1\n", ++kase);else printf("Case #%d: %d\n", ++kase, dis[N]);}return 0;
}

P - The Shortest Path in Nya Graph HDU - 4725相关推荐

  1. hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有n个点和n层,m条边,每一层的任意一个点都可以花费固定的值到下一层或者上一层的任意点 然 ...

  2. HDU - 4725 The Shortest Path in Nya Graph(最短路+思维)

    题目链接:点击查看 题目大意:给定n个点,在一般的基础上给每个点一个维度,也就是多了一个参数表示所在的层次,现在已知: X层的点可以通过增加权值C到达X-1层或X+1层中的任何一个点 给定m个已经形成 ...

  3. hdu 3631 Shortest Path(Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3631 Shortest Path Time Limit: 3000/1000 MS (Java/Oth ...

  4. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  5. AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...

  6. AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和单源最短路径)

    题目链接: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B Single Source Shortest Path ( ...

  7. AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A Single Source Shortest Path In ...

  8. 程序员的算法课(19)-常用的图算法:最短路径(Shortest Path)

    一.最短路径问题 [google笔试题]一个环形公路,给出相邻两点的距离(一个数组),求任意两点的最短距离,要求空间复杂度不超过O(N). 如果从有向图中某一顶点(称为源点)到达另一顶点(称为终点)的 ...

  9. AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A Single Source Shortest Path In ...

最新文章

  1. Linux pip安装包。解决:You should consider upgrading via the 'pip install --upgrade pip' command.
  2. 暂无支持此机型的手机版本_华为AR地图发布重要更新版本 新增对8款机型的支持...
  3. Grafana3整合Zabbix实现图形化监控
  4. mybatis08--关联查询多对一
  5. java开发串口步骤
  6. C#实验室常用软件-Windows Live Writer
  7. linux下启动solr命令,如何自动启动Solr?
  8. 前端学习(2993):vue+element今日头条管理--加入git管理
  9. 一句话超短摘要,速览752篇EMNLP论文
  10. Vue 页面权限控制(一)
  11. 轻松筹 html模板,水滴筹、轻松筹感人标题模板25个字怎么写?在朋友圈发求捐款语录怎么写?...
  12. Mac OS 上MPV播放器常用快捷键
  13. c语言课程设计三色球问题,C++三色球问题描述与算法分析
  14. (个人)太极拳学习系统创新实训第一周(一)
  15. java 上位机 socket_通讯编程上位机软件实现(SOCKET)——第二回
  16. 沉浸式状态栏实现,完美适配Android刘海屏,终极兼容
  17. 从keystore(jks)文件中提取私钥
  18. 上海常英计算机技术有限公司,工科男博士恋上理科女博士 脚踏单车千里求婚...
  19. A5站长网图王到访逐浪软件研发中心
  20. 软件产业未来发展的几个趋势

热门文章

  1. 书值 | 第 2 期:成为技术管理者,思维上应该如何转变?
  2. Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager
  3. API和schema开发过程问题汇总
  4. oracle 监听服务自动停止与无法启动问题
  5. [20150309]使用冷备份做恢复的问题.txt
  6. CLR via C#(第3版):.net中的定时器整理总结
  7. 面试官:你都工作3年了,连选择排序法都不会,我怎么能选择你
  8. amf java_java – 不支持的AMF版本
  9. tabcontainer控件太长_AjaxControlToolKit--TabContainer控件的介绍收藏[摘录]
  10. 喀什市2021年高考成绩查询,2021年新疆高考查分网站查分网址:http://www.xjzk.gov.cn/...