您的位置:首页 > 理论基础 > 数据结构算法

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

2011-11-02 12:17 176 查看
直接贴代码了,可能有错误,如果发现了,请当没看见·················································

#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;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: