您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: