PAT 1003 Universal Travel Sites

19 篇文章 0 订阅
1 篇文章 0 订阅

第一次尝试做顶级的试题,虽说代码还有很多不成熟的地方但能够做出来还是挺高兴的.

考察的是最大流问题

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <stack>
#include <climits>
#include <cstring>
struct AdjVer{
    int id;
    int weight;
    bool hasdelete=false;
};
using namespace std;
int N;
map<string,int> strToInt;
map<int,string> intToStr;
map<int,vector<AdjVer*>> graph;
vector<int> path;
bool flag;
void dfs(int x,int dest, bool* visited){
    visited[x]= true;
    path.push_back(x);
    if (x==dest){
        flag= true;
        return;
    } else{
        for (auto & i : graph[x]) {
            int a=i->id;
            if (!visited[a] && !i->hasdelete){
                dfs(a,dest,visited);
                if (flag){//发现了路径,那么就可以反悔了,不需要弹出
                    return;
                }
            }
        }
        path.pop_back();
    }
}
int findMaxFlow(int src,int dest){
    bool visited[N];
    int sum=0;
    do{
        memset(visited, false, sizeof(visited));
        flag= false;
        path.clear();
        dfs(src,dest,visited);
        vector<int > vector1(path);
//        int size=path.size();//debug时使用,观察path长度
        if (!flag){
            break;//没有找到路径
        }
        int min_Val=INT_MAX;
        for (int i = 0; i < path.size()-1; ++i) {
            int a=path[i];
            int b=path[i+1];
            AdjVer *adjVer = nullptr;
            for(auto x:graph[a]){
                if (x->id==b){
                    adjVer=x;
                    break;
                }
            }
            min_Val=min(min_Val,adjVer->weight);
        }//找到最小值
        sum+=min_Val;
        for (int i = 0; i < path.size()-1; ++i) {
            int a=path[i];
            int b=path[i+1];
            for (auto x:graph[a]){
                if (x->id==b){
                    x->weight-=min_Val;
                    if(x->weight==0){
                        x->hasdelete= true;
                    }
                    break;
                }
            }
            //对路径还需要进行增广操作,也就是反向边增加
            bool hasfind= false;
            for(auto x:graph[b]){
                if (x->id==a){
                    x->weight+=min_Val;
                    hasfind= true;
                }
            }
            if (!hasfind){//增加一条边
                auto* adj=new AdjVer();
                adj->id=a;
                adj->weight=min_Val;
                graph[b].push_back(adj);
            }
        }
    }while (true);
    return sum;
}


int main() {
    string source,dest;
    cin>>source>>dest;
    cin>>N;
    strToInt[source]=0;
    intToStr[0]=source;
    strToInt[dest]=1;
    intToStr[1]=dest;
    int id=2;
    for (int i = 0; i < N; ++i) {
        string a,b;
        int val;
        cin>>a>>b;
        cin>>val;
        if (strToInt.find(a)==strToInt.end()){
            strToInt[a]=id;
            intToStr[id]=a;
            id++;
        }
        if (strToInt.find(b)==strToInt.end()){
            strToInt[b]=id;
            intToStr[id]=b;
            id++;
        }
        auto* adjVer=new AdjVer();
        adjVer->id=strToInt[b];
        adjVer->weight=val;
        graph[strToInt[a]].push_back(adjVer);
    }
    cout<<findMaxFlow(0,1)<<endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值