Candies(POJ 3159)(无负权边的带权有向图或无向图的单源最短路)(Dijkstra)
2016-08-22 19:33
393 查看
http://acm.hust.edu.cn/vjudge/problem/17126
思路:30000点,150000边的稀疏图求单源最短路,读入 “A B C”,就添加A->B的有向边,权值为C,然后求1到N的最短路。
转载自北京大学暑期课课件
思路:30000点,150000边的稀疏图求单源最短路,读入 “A B C”,就添加A->B的有向边,权值为C,然后求1到N的最短路。
#include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <cstring> #include <cmath> #include <vector> #include <map> #include <set> #include <queue> using namespace std; struct CNode { int k; int w; }; bool operator < (const CNode &d1, const CNode &d2) { return d1.w > d2.w; } priority_queue<CNode> pq; bool bUsed[30010] = {0}; vector<vector<CNode> > v; const unsigned int INF = 100000000; int main() { #ifndef ONLINE_JUDGE freopen ("in.txt", "r", stdin); #endif // ONLINE_JUDGE int n, m, a, b, c; CNode p; scanf ("%d%d", &n, &m); v.clear(); v.resize (n + 1); memset (bUsed, 0, sizeof(bUsed)); for (int i = 1; i <= m; i++) { scanf ("%d%d%d", &a, &b, &c); p.k = b; p.w = c; v[a].push_back(p); } p.k = 1; p.w = 0; pq.push(p); while (!pq.empty()) { p = pq.top(); pq.pop(); if (bUsed[p.k]) continue; bUsed[p.k] = true; if (p.k == n) break; for (int i = 0, j = v[p.k].size(); i < j; i++) { CNode q; q.k = v[p.k][i].k; if (bUsed[q.k]) continue; q.w = p.w + v[p.k][i].w; pq.push(q); } } printf ("%d", p.w); return 0; }
转载自北京大学暑期课课件
相关文章推荐
- Poj 3159 Candies(最短路)
- POJ 3159 Candies(差分约束,最短路)
- POJ 3159 Candies(差分约束+dijkstra+heap)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- poj 3159 Candies 邻接表+优先级队列+dijkstra
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- SPFA/Dijkstra POJ 3159 Candies
- POJ 3159 Candies(dijkstra+heap&spfa+stack)
- POJ 3159 Candies (图论,差分约束系统,最短路)
- POJ-3159 Candies 最短路应用(差分约束)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ 3159 Candies(spfa最短路,差分约束)
- poj 3159 dijkstra 最短路
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ 3159 Candies 解题报告(Dijkstra & SPFA) 及 双向Dijkstra性能测试
- poj 3159 Candies(优先队列 dijkstra+堆)
- POJ 3159 Candies(最短路 差分约束)