POJ 3159 Candies
2014-08-21 12:51
302 查看
题目大意:
题目链接
注释代码:
无注释代码:
题目链接
注释代码:
无注释代码:
#include <iostream> #include <cstdio> #include <queue> #include <vector> #define INF 1000000000 #define MAXN 30000 #define MAXM 150000 using namespace std; struct Arc { int v; int c; Arc(void) {} Arc(int vv, int cc) : v(vv), c(cc) {} }; struct Node { int u; int d; Node(void) {} Node(int uu, int dd) : u(uu), d(dd) {} bool operator<(const Node &oth) const { return d > oth.d; } }; Arc arc[MAXM + 1]; int nxt[MAXM + 1]; int head[MAXN + 1]; int e; int d[MAXN + 1]; bool vis[MAXN + 1]; priority_queue<Node> heap; void addarc( int u, int v, int c ) { arc[e].v = v; arc[e].c = c; nxt[e] = head[u]; head[u] = e++; } int dij(int n) { int i; int u, v; Node node; for ( i = 1; i <= n; i++ ) d[i] = INF; d[1] = 0; heap.push(Node(1, 0)); while ( !heap.empty() ) { node = heap.top(); heap.pop(); if ( ( u = node.u ) == n ) return d ; if ( vis[u] ) continue; vis[u] = true; for ( i = head[u]; i; i = nxt[i] ) if ( !vis[ v = arc[i].v ] && d[u] + arc[i].c < d[v] ) { d[v] = d[u] + arc[i].c; heap.push(Node(v, d[v])); } } return d ; } int main() { int n, m; int u, v, c; e = 1; scanf("%d%d", &n, &m); while ( m-- ) { scanf("%d%d%d", &u, &v, &c); addarc( u, v, c ); } printf("%d\n", dij(n)); return 0; }
相关文章推荐
- POJ 3159 Candies 差分约束系统
- poj 3159 Candies
- POJ 3159 Candies(差分约束+dijkstra+heap)
- poj 3159 Candies
- POJ 3159 Candies 差分约束系统(这题卡SPFA的队列的双端队列)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- poj--3159--Candies(简单差分约束)
- poj 3159 Candies 差分约束
- POJ 3159 Candies(差分规划+SPFA)
- poj3159 Candies 2012-09-07
- POJ 3159 Candies(差分约束)
- poj 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies (dij + heap)
- poj 3159 Candies(差分约束)
- poj 3159 Candies(优先队列 dijkstra+堆)
- poj 3159 Candies 差分约束
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ——T 3159 Candies
- POJ 3159 Candies(差分约束)