POJ 3255 Roadblocks——次短路
2018-02-04 10:55
351 查看
这个博主写得很清楚
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef pair<long long, int> P;
const int maxn = 1e4;
const int maxm = 250000;
const int INF = 0x3f3f3f3f;
int n, m, tot, head[maxn];
long long dis1[maxn], dis2[maxn];
struct Edge { int to, cost, next; }edge[maxm];
void init() {
tot = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cost) {
edge[++tot].to = v; edge[tot].cost = cost; edge[tot].next = head[u]; head[u] = tot;
}
long long dijkstra(int s) {
memset(dis1, INF, sizeof(dis1));
memset(dis2, INF, sizeof(dis2));
dis1[s] = 0;
priority_queue<P, vector<P>, greater<P> > q;
q.push(make_pair(0, s));
while (!q.empty()) {
int u = q.top().second, t = q.top().first; q.pop();
if (dis2[u] < t) continue;
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to, cost = edge[i].cost;
if (dis1[v] > t + cost) {
if (dis2[v] > dis1[v]) {
dis2[v] = dis1[v];
q.push(make_pair(dis2[v], v));
}
dis1[v] = t + cost;
q.push(make_pair(dis1[v], v));
}
else if (dis1[v] < t + cost){
if (dis2[v] > t + cost) {
dis2[v] = t + cost;
q.push(make_pair(dis2[v], v));
}
}
}
}
return dis2
;
}
int main() {
while (~scanf("%d %d", &n, &m)) {
init();
int u, v, cost;
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &u, &v, &cost);
addedge(u, v, cost); addedge(v, u, cost);
}
printf("%lld\n", dijkstra(1));
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef pair<long long, int> P;
const int maxn = 1e4;
const int maxm = 250000;
const int INF = 0x3f3f3f3f;
int n, m, tot, head[maxn];
long long dis1[maxn], dis2[maxn];
struct Edge { int to, cost, next; }edge[maxm];
void init() {
tot = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cost) {
edge[++tot].to = v; edge[tot].cost = cost; edge[tot].next = head[u]; head[u] = tot;
}
long long dijkstra(int s) {
memset(dis1, INF, sizeof(dis1));
memset(dis2, INF, sizeof(dis2));
dis1[s] = 0;
priority_queue<P, vector<P>, greater<P> > q;
q.push(make_pair(0, s));
while (!q.empty()) {
int u = q.top().second, t = q.top().first; q.pop();
if (dis2[u] < t) continue;
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to, cost = edge[i].cost;
if (dis1[v] > t + cost) {
if (dis2[v] > dis1[v]) {
dis2[v] = dis1[v];
q.push(make_pair(dis2[v], v));
}
dis1[v] = t + cost;
q.push(make_pair(dis1[v], v));
}
else if (dis1[v] < t + cost){
if (dis2[v] > t + cost) {
dis2[v] = t + cost;
q.push(make_pair(dis2[v], v));
}
}
}
}
return dis2
;
}
int main() {
while (~scanf("%d %d", &n, &m)) {
init();
int u, v, cost;
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &u, &v, &cost);
addedge(u, v, cost); addedge(v, u, cost);
}
printf("%lld\n", dijkstra(1));
}
return 0;
}
相关文章推荐
- poj 3255 Roadblocks(spfa次短路 || Dijsktra)
- POJ 3255 Roadblocks(A*求次短路)
- POJ 3255 Roadblocks (次短路问题)
- POJ 3255 Roadblocks(Dijstra 求次短路长度)
- POJ3255-Roadblocks-dijkstra求次短路
- poj 3255 Roadblocks(无向图次短路 SPFA)
- [POJ 3255]Roadblocks[dijkstra][次短路]
- POJ 3255 - Roadblocks(次短路)
- POJ 3255 Roadblocks 【次短路】
- POJ3255 - Roadblocks - 次短路(spfa+LLL优化)
- 【POJ 3255】Roadblocks 【次短路】
- 笔记③:POJ Roadblocks 次短路问题代码解析(优先队列逆序排列两种方法)
- poj 3255 Roadblocks
- poj Roadblocks 次短路(数据较大)
- POJ - 3255 - Roadblocks(Dijkstra)
- POJ 3255 Roadblocks --次短路径
- Roadblocks POJ - 3255 (次段路)
- POJ - 3255 Roadblocks —— 次短路
- POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks
- poj 3255 Roadblocks