POJ 3159 Candies (差分约束#1)
来源:互联网 发布:飞行器发动机 知乎 编辑:程序博客网 时间:2024/05/01 22:49
用了两种方法:用栈的SPFA和用优先队列的Dijkstra都是500ms+
#include <cstdio>#include <algorithm>#include <queue> #include <cstdlib> #include <cstring> #include <stack> #define N 30005#define M 150005using namespace std; const int inf = (-1u >> 1); int n, m;typedef pair<int, int> pii; int first[N], d[N]; int next[M], u[M], v[M], w[M];bool inq[N];int SPFA(int s, int n)//用栈 { for (int i = 1; i <= n; i++)d[i] = inf; d[s] = 0; stack<int> q; memset(inq, false, sizeof(inq)); q.push(s); while (!q.empty()) { int x = q.top();q.pop(); inq[x] = false; for (int e = first[x]; e != -1; e = next[e])if (d[v[e]] > d[u[e]] + w[e]) { d[v[e]] = d[u[e]] + w[e]; if (!inq[v[e]]) { inq[v[e]] = true; q.push(v[e]); } } } return d[n]; }void dijkstra(int s, int n){ for (int i = 1;i <= n; i++)d[i] = inf; d[s] = 0; priority_queue<pii, vector<pii>, greater<pii> > q; q.push(make_pair(d[s], s)); while (!q.empty()) { pii u = q.top();q.pop(); int x = u.second; if (u.first != d[x])continue; for (int e = first[x]; e != -1; e = next[e])if (d[v[e]] > d[x] + w[e]) { d[v[e]] = d[x] + w[e]; q.push(make_pair(d[v[e]], v[e])); } }} int main(){ // FILE* fp = fopen("in.txt", "r"); while (scanf( "%d", &n) != EOF && n) { scanf( "%d", &m); int a, b, c; int e = 0; for (int i = 1;i <= n ; i++)first[i] = -1; for (int i = 0; i < m; i++) { scanf( "%d %d %d", &a, &b, &c); next[e] = first[a]; first[a] = e; w[e] = c; u[e] = a, v[e] = b; e++; } dijkstra(1, n); printf("%d\n", d[n]); } return 0;}
- poj 3159 Candies(差分约束)
- POJ 3159Candies (差分约束)
- POJ 3159 Candies (差分约束)
- poj 3159 Candies(差分约束)
- POJ 3159 Candies (差分约束#1)
- POJ 3159 Candies(差分约束系统)
- 【POJ 3159】 Candies 差分约束系统
- POJ 3159 Candies 差分约束+spfa
- POJ--3159[Candies] 差分约束
- poj 3159 Candies 差分约束 + spfa
- poj-3159-Candies-差分约束+stack
- POJ 3159 Candies 差分约束系统
- poj 3159 Candies 差分约束
- POJ 3159 Candies 差分约束
- POJ 3159 Candies(差分约束)
- POJ 3159 Candies(差分约束系统)
- POJ 3159 Candies(差分约束)
- POJ 3159 Candies 差分约束
- HDU 2105 The Center of Gravity
- ftp,nfs,samba基本配置
- Android修改自己程序字体
- STL之vector容器
- 程序员看世界之排队现象(7)2012-7-31
- POJ 3159 Candies (差分约束#1)
- linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结(tar包解压)
- 输秒求天
- 维特比算法一点个人理解
- 合格,良好,优秀的程序员
- ASP.NET访问权限管理
- c# winform 服务器端创建 文件夹
- Poj _1364 King(差分约束#2)
- Android Animation