51nod 1649 齐头并进 (djikstra求最短路径,只用跑一次)
来源:互联网 发布:网络脱口秀节目排行榜 编辑:程序博客网 时间:2024/05/22 00:53
题目:
这道题有一个坑点:两种交通工具同时出发,中途不能停留在同一个小镇。
其实想通了就很简单,因为要么火车一步到达,要么汽车一步到达。不可能停留在同一个地方。
可是我还WA了好几次,蠢哭。想用BFS写,一直TLE,后来想到这点之后,用djikstra求单源最短路径就出来了。
如果火车一步到,就求汽车的单源最短路径;如果汽车一步到,就求火车的单源最短路径。
代码:
#include <iostream>#include <algorithm>#include <map>#include <vector>#include <set>#include <math.h>#include <queue>#include <assert.h>#include <stdio.h>#include <stdlib.h>using namespace std;typedef long long ll;//#define INF 2147483647#define INF 2000000000int n,m;#define MAX_V 410int cost[MAX_V][MAX_V]; //cost[u][v]表示e = (u,v)的权值 int d[MAX_V]; //源点s出发的最短距离 bool used[MAX_V]; //标记使用过的点 int djikstra(){ fill(d,d+n+1,INF); fill(used,used+n,false); d[1] = 0; while(true){ int v = -1; for(int i = 1;i <= n; i++){ if(!used[i]&&(v == -1 || d[i] < d[v])) v = i; } if(v == -1) break; used[v] = true; for(int i = 1;i <= n; i++){ if(cost[v][i] == 1){ d[i] = min(d[i],d[v]+cost[v][i]); } } } if(d[n] == INF) return -1; else return d[n];}int main() { cin >> n >> m; for(int i = 1;i <= n; i++){ for(int j = 1;j <= n; j++){ cost[i][j] = -1; if(i == j) cost[i][j] = 0; } } for(int i = 1;i <= m; i++){ int u,v; cin >> u >> v; cost[u][v] = 1; cost[v][u] = 1; } if(cost[1][n] == 1){ for(int i = 1;i <= n; i++){ for(int j = 1;j <= n; j++){ cost[i][j] = -cost[i][j]; } } } cout << djikstra() << endl; return 0;}
阅读全文
0 0
- 51nod 1649 齐头并进 (djikstra求最短路径,只用跑一次)
- 51Nod-1649-齐头并进
- 51nod 1649 齐头并进 (最短路)
- 51Nod 1649 齐头并进 最短路
- 51nod 1649 齐头并进 (两次dijkstra求最短路)
- 最短路径之djikstra algorithm
- 51nod 可能的路径
- 【51Nod 1610】路径计数
- 51Nod-1967-路径定向
- 51nod 1610 路径计数
- 51Nod-1610-路径计数
- 51nod 1610 路径计数
- 只用一次+ 求三个整数之和
- poj1797--djikstra
- Djikstra算法
- 51nod 1443 路径和树
- 51NOD 1274 最长递增路径
- 51nod 1247 可能的路径
- 彻底剖析numpy的数据类型
- 51nod 1562 玻璃切割 (STL map+一点点的思考)
- 机器学习笔记
- vim 配置
- 11.19
- 51nod 1649 齐头并进 (djikstra求最短路径,只用跑一次)
- fjnuoj1193 布置会场( 快速幂 矩阵)
- opencv-图像抠图
- TCP/IP网络编程学习之路二
- 剔除重复数据
- 刷题蓝桥杯(java)---Huffman树
- 数据库的操作
- Bailian2888 字符串中的数字【字符串】
- 信任的机制——区块链