大意:给定一张无向图,让以最少的次数将起点的全部乘客运往终点,图中的每个节点有容量限制。
思路:与UVA 10048 差不多,都可以用Floyd动态规划去解决。
d[i][j] = max(d[i][j], min(d[i][k], d[k][j]));
另外:导游也算一个容量。
CODE:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 110
int d[MAXN][MAXN];
int n, m;
void init()
{
memset(d, 0, sizeof(d));
}
void Floyd()
{
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
d[i][j] = max(d[i][j], min(d[i][k],d[k][j]));
}
}
int main()
{
int times = 0;
while(scanf("%d%d", &n, &m) && (n || m))
{
init();
while(m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
d[u][v] = d[v][u] = w;
}
int s, e, people;
scanf("%d%d%d", &s, &e, &people);
Floyd();
int ans;
if(people % (d[s][e]-1)) //导游也占一个容量
{
ans = people/(d[s][e]-1) + 1;
}
else
{
ans = people/(d[s][e]-1);
}
printf("Scenario #%d\n", ++times);
printf("Minimum Number of Trips = %d\n\n", ans);
}
return 0;
}
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 110
int d[MAXN][MAXN];
int n, m;
void init()
{
memset(d, 0, sizeof(d));
}
void Floyd()
{
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
d[i][j] = max(d[i][j], min(d[i][k],d[k][j]));
}
}
int main()
{
int times = 0;
while(scanf("%d%d", &n, &m) && (n || m))
{
init();
while(m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
d[u][v] = d[v][u] = w;
}
int s, e, people;
scanf("%d%d%d", &s, &e, &people);
Floyd();
int ans;
if(people % (d[s][e]-1)) //导游也占一个容量
{
ans = people/(d[s][e]-1) + 1;
}
else
{
ans = people/(d[s][e]-1);
}
printf("Scenario #%d\n", ++times);
printf("Minimum Number of Trips = %d\n\n", ans);
}
return 0;
}