哈工大数据结构实验3 图的应用

直接贴代码了,可能有错误,如果发现了,请当没看见·················································

#include <iostream>
#include <vector>

#define MAX 100
using namespace std;
class node{
    public:
     int to;
     int len;
}t;
int from,to,len;
vector<vector <node> > myVec(MAX);
int findWay(int from,int to){
    vector<int> tVec;
    bool check[MAX];
    int minLen[MAX];
    cout<<"The Min Way Is:";
    memset(minLen,0,sizeof(minLen));
    memset(check,0,sizeof(check));
    check[0]=true;
    tVec.clear();
    tVec.push_back(from);
    while(tVec.size()!=0){
        int node = tVec[0];
        tVec.erase(tVec.begin());
        for(int i=0;i<myVec[node].size();i++){
            t=myVec[node][i];
            //if (!check[t.to]){
                    if (t.len+minLen[node]<minLen[t.to]||minLen[t.to]==0){
                        minLen[t.to]=t.len+minLen[node];
                        tVec.push_back(t.to);
                        check[t.to]=true;
                    }
            //}
        }
    }
    return minLen[to];
}
int main()
{
    while(true){
        cin>>from;
        if (from==-1){
             cout<<"Input Over"<<endl;
             break;
        }
        cin>>to>>len;
        t.to=to;
        t.len=len;
        myVec[from].push_back(t);
    }
    while(true){
        cin>>from>>to;
        cout<<findWay(from,to)<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值