Description
The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. She'll traverse some or all of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be multiply connected with different length roads. All farms are connected one way or another to Farm 1.
Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she's only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry.
Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she'll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she'll have to traverse.
Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she's only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry.
Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she'll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she'll have to traverse.
Input
* Line 1: Two space-separated integers, N and M.
* Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.
* Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.
Output
* Line 1: A single integer that is the length of the longest road required to be traversed.
Sample Input
3 3 1 2 23 2 3 1000 1 3 43
Sample Output
43
/*
接下来是用优先队列优化的
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 2008
bool vis[maxn];
int dis[maxn];
int n,m;
struct Edge
{
int v,w;
bool operator < (const Edge & a) const
{
return w > a.w;
}
}edge;
vector <Edge> ans[maxn];
inline int max(int a,int b)
{
return a>b?a:b;
}
int heapprim()
{
int nown = 0;
int cost = 0;
edge.v = 1;
edge.w = 0;
priority_queue <Edge> q;
q.push(edge);
while(nown < n && !q.empty())
{
L:
edge = q.top();
q.pop();
while(vis[edge.v] && !q.empty()) goto L;
if(!vis[edge.v])
{
nown++;
int nowv = edge.v;
vis[nowv] = 1;
cost = max(edge.w,cost);
for(int i=0;i<ans[nowv].size();i++)
{
if(!vis[ans[nowv][i].v] && dis[ans[nowv][i].v] > ans[nowv][i].w)
{
dis[ans[nowv][i].v] = ans[nowv][i].w;
edge.v = ans[nowv][i].v;
edge.w = ans[nowv][i].w;
q.push(edge);
}
}
}
}
return cost;
}
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
int u,v,w;
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edge.v = v;
edge.w = w;
ans[u].push_back(edge);
edge.v = u;
ans[v].push_back(edge);
}
printf("%d\n",heapprim());
}
return 0;
}
Kruskal算法:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 2008
int father[maxn];
int rank1[maxn];
int find(int x)
{
if(x == father[x])return x;
return father[x] = find(father[x]);
}
struct Edge
{
int from,to,key;
}edge[10008];
bool cmp(Edge a,Edge b)
{
return a.key < b.key;
}
void Union(int a,int b)
{
if(rank1[a] > rank1[b])
{
father[b] = a;
}
else if(rank1[a] == rank1[b])
{
father[b] = a;
rank1[a]++;
}
else
{
father[a] = b;
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
memset(rank1,0,sizeof(rank1));
for(int i=1;i<=n;i++)
{
father[i] = i;
}
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].key);
}
sort(edge+1,edge+m+1,cmp);
int cost = 0;
for(int i=1;i<=m;i++)
{
int uu = find(edge[i].from);
int vv = find(edge[i].to);
if(uu != vv)
{
Union(uu,vv);
cost = edge[i].key;
}
}
printf("%d\n",cost);
}
return 0;
}
/* 接下来是不用优先队列优化的 */ #include <iostream> #include <cstring> #include <cstdio> using namespace std; #define maxn 2008 #define inf 0x3f3f3f3f int W[maxn][maxn],dis[maxn]; bool vis[maxn]; int main() { int n,m; while(scanf("%d%d",&n,&m)==2) { int u,v,w; memset(W,0x3f,sizeof(W)); memset(vis,0,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); for(int i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); W[u][v] = W[v][u] = W[u][v]>w?w:W[u][v]; } int cost = 0; dis[1] = 0; for(int i=1;i<=n;i++) { int minlen = inf; int nowv; for(int j=1;j<=n;j++) { if(!vis[j] && dis[j] < minlen) { minlen = dis[j]; nowv = j; } } cost = cost > minlen?cost:minlen; vis[nowv] = 1; for(int j=1;j<=n;j++) { if(!vis[j] && dis[j] > W[nowv][j]) { dis[j] = W[nowv][j]; } } } printf("%d\n",cost); } return 0; }