Heavy Transportation(POJ 1793)
写这题被坑了好几次,这题解法思路就是在最短路中用权值最大的边为基础不断更新可以用朴素dijkstra,堆优化dijkstra, spfa,kurskal等。 注意:1.数据量大要用cin,cout会TLE 2.图是无向图不是有向图 3.输出格式记得每组答案后面有两回车
1.朴素版Dijkstra
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1010, INF = 0x3f3f3f3f;
int g[N][N];
int dist[N];
bool st[N];
int dijkstra(int n)
{
memset(st, false, sizeof st);
memset(dist, -1, sizeof dist);
dist[1] = INF;
for(int i = 1; i < n; ++i)
{
int t = -1;
for(int j = 1; j <= n; ++j)
if(!st[j] && (t == -1 || dist[j] > dist[t]))
t = j;
st[t] = true;
for(int j = 1; j <= n; ++j)
dist[j] = max(dist[j], min(dist[t], g[t][j]));
}
return dist[n];
}
int main()
{
int T;
scanf("%d", &T);
for(int i = 1; i <= T; ++i)
{
int n, m;
scanf("%d%d", &n, &m);
memset(g, -1, sizeof g);
while(m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
g[a][b] = g[b][a] = max(g[b][a], c);
}
printf("Scenario #%d:\n%d\n\n", i, dijkstra(n));
}
return 0;
}
朴素版Dijkstra耗时如下
2.堆优化版Dijkstra
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010, M = 1e5, INF = 0x3f3f3f3f;
int e[M], w[M], ne[M], h[N], idx;
int dist[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void init()
{
idx = 0;
memset(h, -1, sizeof h);
}
int dijkstra(int n)
{
memset(dist, -1, sizeof dist);
memset(st, false, sizeof st);
dist[1] = INF;
priority_queue<PII> heap;
heap.push({dist[1], 1});
while(!heap.empty())
{
int key = heap.top().second;
heap.pop();
if(st[key]) continue;
st[key] = true;
for(int i = h[key]; i != -1; i = ne[i])
{
int &j = e[i];
if(dist[j] < min(dist[key], w[i]))
{
dist[j] = min(dist[key], w[i]);
heap.push({dist[j], j});
}
}
}
return dist[n];
}
int main()
{
int T;
scanf("%d", &T);
for(int i = 1; i <= T; ++i)
{
init();
int n, m;
scanf("%d%d", &n, &m);
while(m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
printf("Scenario #%d:\n%d\n\n", i, dijkstra(n));
}
return 0;
}
看起来堆优化版Dijkstra效果不佳啊
3.Kruskal版
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1010, M = 1e5, INF = 0x3f3f3f3f;
struct Edge{
int u, v, w;
bool operator < (const Edge& t) const {
return w > t.w;
}
}edges[M];
int p[N];
int find(int u){
return u == p[u] ? p[u] : p[u] = find(p[u]);
}
int main()
{
int T;
scanf("%d", &T);
for(int i = 1; i <= T; ++i)
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) p[i] = i;
for(int j = 0; j < m; ++j)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
edges[j] = {a, b, c};
}
sort(edges, edges + m);
int ans = INF;
for(int j = 0; j < m && find(1) != find(n); ++j)
{
int &u = edges[j].u, &v = edges[j].v, &w = edges[j].w;
int p1 = find(u), p2 = find(v);
if(p1 != p2)
{
p[p1] = p2;
ans = min(ans, w);
}
}
printf("Scenario #%d:\n%d\n\n", i, ans);
}
return 0;
}
Kruskal还算快,写起来也方便
4.SPFA版本
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1010, M = 1e5 + 10;
int dist[N];
bool st[N];
int e[M], w[M], ne[M], h[N], idx;
void init()
{
idx = 0;
memset(h, -1, sizeof h);
}
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int spfa(int n)
{
memset(dist, -1, sizeof dist);
memset(st, false, sizeof st);
dist[1] = 1e9;
queue<int> q;
q.push(1);
st[1] = true;
while(!q.empty())
{
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; i != -1; i = ne[i])
{
int &j = e[i];
if(dist[j] < min(dist[t], w[i]))
{
dist[j] = min(dist[t], w[i]);
if(!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
return dist[n];
}
int main()
{
int T;
scanf("%d", &T);
for(int i = 1; i <= T; ++i)
{
int n, m;
scanf("%d%d", &n, &m);
init();
while(m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
printf("Scenario #%d:\n%d\n\n", i, spfa(n));
}
return 0;
}
好吧,快速最短路算法有点大失所望了
Tips: 朝着区域赛冲!!!!!!!!!
|