POJ
来源:互联网 发布:can数据帧格式 波形 编辑:程序博客网 时间:2024/05/19 10:41
一开始WA,到网上看了看貌似是重边的问题,但题目中的重边不是最新的那一条而是最小的那一条,我的dijkstra无所畏惧,最后看了看原来是输入写反了、、、、、、
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef pair<int, int> P;const int INF = 0x3f3f3f3f;const int maxn = 2010;int n, m, u, v, cost, d[maxn], vis[maxn];vector<P> g[maxn];void dijkstra() { for (int i = 1; i <= n; i++) { d[i] = INF, vis[i] = 0; } d[1] = 0; priority_queue<P, vector<P>, greater<P> > q; q.push(make_pair(0, 1)); while(!q.empty()) { u = q.top().second; q.pop(); if(vis[u]) continue; vis[u] = 1; for (int i = 0; i < g[u].size(); i++) { v = g[u][i].first, cost = g[u][i].second; if (d[v] > d[u] + cost) { d[v] = d[u] + cost; q.push(make_pair(d[v], v)); } } }}int main() { while (~scanf("%d %d", &m, &n)) { for (int i = 1; i <= n; i++) g[i].clear(); for (int i = 1; i <= m; i++) { scanf("%d %d %d", &u, &v, &cost); g[u].push_back(make_pair(v, cost)); g[v].push_back(make_pair(u, cost)); } dijkstra(); printf("%d\n", d[n]); } return 0;}
阅读全文
1 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 浅谈C语言中的位段
- 使用 Device Mapper来改变Docker 容器的大小
- 数据库系统概要
- 一个好看的弹出提示框样式,toastr插件的使用及动态展示分析
- 发布AndroidLibrary到JCenter,一步一步,拒绝踩坑!
- POJ
- FreeRTOS笔记2
- 时光机——svn概念篇
- Form habits----思维方式训练
- docker网络配置方法总结
- 堆中的路径
- getchar()输入不定长数组,getline输入字符串,int和string的相互转换
- 中介者模式
- 设备树OF API