题意:有一个导游,要带领团队从当前点,走到目标点,但是每条路一次性通过的人有数量限制,
所以需要分批次进行。问最少需要分多少次进行。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define ll 0x7fffffff
using namespace std;
typedef struct d_node
{
int point1;
int point2;
int weight;
}enode;
enode edge[10001];//边,输入存储
//union_set
int sets[101];//点的索引
int Rank[101];//那个点作为根节点
void set_inital( int a, int b )
{
for ( int i = a ; i <= b ; ++ i ) {
Rank[i] = 0;
sets[i] = i;
}
}
int set_find( int a )
{
if ( a != sets[a] )
sets[a] = set_find( sets[a] );
return sets[a];
}
void Set_union( int a, int b )
{
if ( Rank[a] < Rank[b] )
sets[a] = b;
else {
if ( Rank[a] == Rank[b] )
Rank[a] ++;
sets[b] = a;
}
}
//end_union_set
int cmp_e( enode a, enode b )
{
return a.weight > b.weight;
}
int Vector[101];//通过此点的最小权值
int kruskal( int n, int r, int s, int t )
{
set_inital( 1, n );
for ( int i = 1 ; i <= n ; ++ i )
Vector[i] = ll;
//cout<<vector[1]<<endl;
sort( edge, edge+r, cmp_e );
for ( int i = 0 ; i < r ; ++ i ) {
int A = set_find( edge[i].point1 );
int B = set_find( edge[i].point2 );
if ( A != B ) {
Set_union( A, B );
int mid = min( Vector[A], Vector[B] );
Vector[set_find(A)] = min( mid, edge[i].weight );
if ( set_find( s ) == set_find( t ) )
return Vector[set_find(s)];
}
}
return -1;
}
int main()
{
int N,R,a,b,c,s,t,p,C = 1;
while ( scanf("%d%d",&N,&R) && N+R ) {
for ( int i = 0 ; i < R ; ++ i ) {
scanf("%d%d%d",&a,&b,&c);
edge[i].point1 = a;
edge[i].point2 = b;
edge[i].weight = c;
}
scanf("%d%d%d",&s,&t,&p);
int W = kruskal( N, R, s, t );
int A = p/(W-1) + (p%(W-1) != 0);
printf("Scenario #%d\nMinimum Number of Trips = %d\n\n",C ++,A);
}
return 0;
}