uva 10099 The Tourist Guide

原题
Mr. G. works as a tourist guide. His current assignment is to take some tourists from one city to
another. Some two-way roads connect the cities. For each pair of neighboring cities there is a bus
service that runs only between those two cities and uses the road that directly connects them. Each
bus service has a limit on the maximum number of passengers it can carry. Mr. G. has a map showing
the cities and the roads connecting them. He also has the information regarding each bus service. He
understands that it may not always be possible for him to take all the tourists to the destination city
in a single trip. For example, consider the following road map of 7 cities. The edges connecting the
cities represent the roads and the number written on each edge indicates the passenger limit of the bus
service that runs on that road。
uva 10099 The Tourist Guide_第1张图片
Now, if he wants to take 99 tourists from city 1 to city 7, he will require at least 5 trips and the
route he should take is : 1 - 2 - 4 - 7.
But, Mr. G. finds it difficult to find the best route all by himself so that he may be able to take all
the tourists to the destination city in minimum number of trips. So, he seeks your help.
Input
The input will contain one or more test cases. The first line of each test case will contain two integers:
N (N ≤ 100) and R representing respectively the number of cities and the number of road segments.
Then R lines will follow each containing three integers: C1, C2and P. C1and C2are the city numbers
and P (P > 1) is the limit on the maximum number of passengers to be carried by the bus service
between the two cities. City numbers are positive integers ranging from 1 to N. The (R + 1)-th line
will contain three integers: S, D and T representing respectively the starting city, the destination city
and the number of tourists to be guided.
The input will end with two zeroes for N and R.
Output
For each test case in the input first output the scenario number. Then output the minimum number of
trips required for this case on a separate line. Print a blank line after the output of each test case.
Sample Input
7 10
1 2 30
1 3 15
1 4 10
2 4 25
2 5 60
3 4 40
3 6 20
4 7 35
5 7 20
6 7 30
1 7 99
0 0
Sample Output
Scenario #1
Minimum Number of Trips = 5
题目大意
给你个图,然后给你起点和终点和人数,问你导游把这群人从起点带到终点得用多少次。(坑)导游也算一个人,导游回来不算次数,导游最后也得到终点。
思路见代码向下方

代码1

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int N=102;
const int maxint=9999999;
struct edge
{
    int u,v,cost;
};
edge es[10001];
int Matrix[N][N];
bool mark[N][N];
stack<int> si;
bool cmp(const edge &e1,const edge &e2)
{
    return e1.cost>e2.cost;
}
int n,r;
int c1,c2,p;
int s,d,t,res;
int father[N],rank[N];
void ini(int x)
{
    res=maxint;
    memset(mark,0,sizeof(mark));
    for(int i=1;i<=x;i++)
    {
        father[i]=i;
        rank[i]=0;
        for(int j=1;j<=x;j++)
        Matrix[i][j]=maxint;
    }
}
int Find(int x)
{
    if(father[x]==x)
    return x;
    else
    return father[x]=Find(father[x]);
}
bool merge(int a,int b)
{
    int x=Find(a);
    int y=Find(b);
    if(x==y)
    return false;
    if(rank[x]else
    {
        father[y]=x;
        if(rank[x]==rank[y])
        rank[y]++;
    }
    return true;
}
void kruskal(int x)
{
    sort(es+1,es+1+x,cmp);
    for(int i=1;i<=x;i++)
    {
        edge e=es[i];
        if(merge(e.v,e.u))
        Matrix[e.v][e.u]=Matrix[e.u][e.v]=e.cost;
    }
}
bool dfs(int x)
{
    if(x==d)
    return true;
    for(int i=1;i<=n;i++)
    {
        if(Matrix[x][i]!=maxint&&!mark[x][i]&&i!=x)
        {
            mark[x][i]=mark[i][x]=true;
            si.push(Matrix[x][i]);
            if(dfs(i))
            return true;
            else
            {
                si.pop();
            }
        }
    }
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    int k=1;
    while(cin>>n>>r)
    {
        while(!si.empty())
        si.pop();
        if(n+r==0)
        break;
        for(int i=1;i<=r;i++)
        {
            cin>>c1>>c2>>p;
            es[i].u=c1;es[i].v=c2;es[i].cost=p;
        }
        ini(n);
        cin>>s>>d>>t;
        kruskal(r);
/*      for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            cout<
        dfs(s);
        while(!si.empty())
        {
            res=min(res,si.top());
            si.pop();
        }
        res--;
        if(t%res)
        {
            cout<<"Scenario #"<cout<<"Minimum Number of Trips = "<1<else
        {

            cout<<"Scenario #"<cout<<"Minimum Number of Trips = "<cout<return 0;
}




代码2

#include
#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include
using namespace std;
const int N=103;
const int maxint=999999;
int Matrix[N][N];
int n,r;
int c1,c2,p;
int s,t,d;
int floyed(int x,int a,int b)
{
    int res=maxint;
    for(int k=1;k<=x;k++)
    {
        for(int i=1;i<=x;i++)
        {
            for(int j=1;j<=x;j++)
            {
                Matrix[i][j]=max(Matrix[i][j],min(Matrix[i][k],Matrix[k][j]));
            }
        }
    }
    return Matrix[a][b];
}
int main()
{
    ios::sync_with_stdio(false);
    int k=1;
    while(cin>>n>>r,n+r)
    {
        memset(Matrix,0,sizeof(Matrix));
        for(int i=1;i<=r;i++)
        {
            cin>>c1>>c2>>p;
            Matrix[c1][c2]=Matrix[c2][c1]=p;
        }
        cin>>s>>d>>t;
        int ans=floyed(n,s,d);
        ans--;
        if(t%ans)
        {
            cout<<"Scenario #"<cout<<"Minimum Number of Trips = "<1<else
        {

            cout<<"Scenario #"<cout<<"Minimum Number of Trips = "<cout<return 0;
}










思路
这题真是天坑啊,我最先写的代码1,首先找出这个图的最大生成树,然后就能得到从起点到到终点一个唯一最优路径。然后用深度优先搜索和栈记录路径节点,结果一顿wa。后来写了代码2,用floyed,速度虽然慢了一点,但还是wa。醉了,自己搞了n个数据都过了,还是没用,最后上网上找发现最后一定要加一个换行,否则就wa。加个换行以后就都过了~~ 晕

你可能感兴趣的:(数据结构,图论)