# 10099 - The Tourist Guide

kruskal算法代码如下：（AC）

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define MAXIN 10009

struct edge {
int u, v, w;
}e[MAXIN];
int N, R, S, D, T;
int f[110];

bool cmp(const edge& e1, const edge& e2)
{
return e1.w > e2.w;
}

int father(int n)
{
return n == f[n] ? n : f[n] = father(f[n]);
}

int Kruskal()
{
for (int i = 1; i <= N; i++)
f[i] = i;

sort(e, e + R, cmp);

for (int i = 0; i < R; i++)
{
int l, r;
l = father(e[i].u);
r = father(e[i].v);
if (l != r)
{
f[l] = r;
if (father(S) == father(D))
return e[i].w;
}

}
return 0;

}

int main()
{
int cas = 0;
for (;;)
{
cin >> N >> R;
if (!N && !R)return 0;
cas++;
for (int i = 0; i < R; i++)
{
cin >> e[i].u >> e[i].v >> e[i].w;
}
cin >> S >> D >> T;

int cap = Kruskal() - 1;
int ans = 0, total = 0;
while (ans < T)
{
ans += cap;
total++;
}
cout << "Scenario #" << cas << endl;
cout << "Minimum Number of Trips = " << total << endl<<endl;
}

}


kruskal算法的核心步骤：

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int G[100][100],visit[100];
int N, R,S, D, T,cap;
int cas = 0;

void DFS(int s,int d,int m)
{
if (s == d)
{
cap = cap > m ? cap : m;
return;
}

visit[s] = 1;
for(int i=1;i<=N;i++)
if (G[s][i]&&!visit[i])
{
if (G[s][i] < m)
DFS(i, d, G[s][i]);
else
DFS(i, d, m);
}
visit[s] = 0;
}

void Init()
{
memset(G, 0, sizeof(G));
memset(visit, 0, sizeof(visit));
for (int i = 0; i < R; i++)
{
int x, y,z;
cin >> x >> y >> z;
G[y][x] = G[x][y] = z;
}
cin >> S >> D >> T;

cap = -(1 << 20);
}

void solve()
{

int ans = 0; int j = 0;
while (ans < T)
{
ans += (cap-1);
j++;
}

cas++;
cout << "Scenario #" << cas << endl;
cout << "Minimum Number of Trips = " << j << endl;

}

int main()
{

freopen("c:\\data\\10099.txt", "r", stdin);

for (;;)
{
cin >> N >> R;
if (N == 0 && R == 0)return 0;
Init();
DFS(S, D,1<<20);
solve();

}
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：10099 - The Tourist Guide 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)