The Tourist Guide(uva)



                              The Tourist Guide


目链接:Click Here~

题目分析:

    为什么要写这个简单题的解题报告呢?是因为看到网上太多的抄袭博客了,一个人写了,然后很多人跟着ctrl+c,ctrl+v。因为我也不是大牛,因为真正的大牛是不写博客的。但是我个人觉得我的图论知识还是可以得。所以我想说说这道题。这道题其实真正的算法叫做瓶颈树的算法。而瓶颈树由可以用最小生成树解出。因为在建树的过程就满足了最小瓶颈树的特性。但是这题要改成最大生成数的最小瓶颈树来解。而这也简单,只要保证题目中给出的权值没有负数的时候就可以把权值转换成负数来求解最大生成树。

   这题的另一个算法就是很裸的最大流。感兴趣的童鞋可以写写。

#include 
#include 
#include 
#include 
#include 
using namespace std;

const int MAXV = 100+10;
const int MAXINT = 999999;
struct Edge{
   int v;
   int weight;
   Edge(int _v,int _w):v(_v),weight(_w){};
};
class PRIM{
public:
    void Init();
    inline void AddEdge(int x,int y,int w);
    bool ReadGraph();
    int PutAnswer(int sum);
    int Prim();
private:
    vectoredges;
    vector G[MAXV];
    int n,m;
    int S,T,D;
};
void PRIM::Init()
{
    for(int i = 0;i < n;++i)
        G[i].clear();
    edges.clear();
}
void PRIM::AddEdge(int x,int y,int w)
{
    edges.push_back(Edge(y,w));
    int sz = edges.size();
    G[x].push_back(sz-1);
}
bool PRIM::ReadGraph()
{
    while(scanf("%d%d",&n,&m),(n||m))
    {
        Init();
        int x,y,w;
        for(int i = 0;i < m;++i){
            scanf("%d%d%d",&x,&y,&w);
            AddEdge(x,y,-w);
            AddEdge(y,x,-w);
        }
        scanf("%d%d%d",&S,&T,&D);
        return true;
    }
    return false;
}
int PRIM::Prim()
{
    int v,d;
    int ans = -MAXINT;
    int dist[MAXV];
    int parent[MAXV];
    bool intree[MAXV];
    for(int i = 0;i <= n;++i){
        dist[i] = MAXINT;
        intree[i] = false;
    }

    dist[v = S] = 0;
    while(intree[v] == false){
        intree[v] = true;
        for(int i = 0;i < (int)G[v].size();++i){
            int u = G[v][i];
            Edge& e = edges[u];
            if((dist[e.v] > e.weight)&&(false == intree[e.v])){
                dist[e.v] = e.weight;
                parent[e.v] = v;
            }
        }

        v = 1;
        d = MAXINT;
        for(int i = 1;i <= n;++i){
            if((false == intree[i])&&(d > dist[i])){
                d = dist[i];
                v = i;
            }
        }
        ans = max(ans,d);
        if(v == T)
            break;
    }
    return -ans;
}
int PRIM::PutAnswer(int capcity)
{
    if(S == T)
       return 0;

    capcity--;
    int ans;
    ans = D/capcity;
    if(D%capcity) ans++;
    return ans;
}
int main()
{
    PRIM prim;
    int kase = 1;
    while(prim.ReadGraph()){
        cout<<"Scenario #"<

你可能感兴趣的:(其他图)