使用弗洛伊德求最短路径,并记录路径

#include <bits/stdc++.h> //作者:hyc
using namespace std;
struct node_city
{
    int city;
    double x;
    double y;
};
struct link
{
    int citya;
    int cityb;
};
struct floyd
{
    vector<vector<double> >Graph;
    vector<vector<int> >Path;
};
vector<node_city> getcity() //存储所有的城市信息
{
    vector<node_city> result;
    ifstream infile;
    string path="city.txt";
    infile.open(path.data());
    assert(infile.is_open());
    string citytemp;
    while (getline(infile,citytemp))
    {
        istringstream is(citytemp);
        while (!is.eof())
        {
            node_city temp;
            is>>temp.city;
            is>>temp.x;
            is>>temp.y;
            result.push_back(temp);
            citytemp.clear();
        }
    }
    infile.close();
    return result;
}
vector<link> getlink() //存储所有的边信息
{
    vector<link> result;
    ifstream infile;
    string path="link.txt";
    infile.open(path.data());
    assert(infile.is_open());
    string linktemp;
    while (getline(infile,linktemp))
    {
        istringstream is(linktemp);
        while (!is.eof())
        {
            link temp;
            is>>temp.citya;
            is>>temp.cityb;
            result.push_back(temp);
            linktemp.clear();
        }
    }
    infile.close();
    return result;
}
double getdistance(node_city a,node_city b) //计算两个城市之间距离
{
    double xx=(b.x-a.x)*(b.x-a.x);
    double yy=(b.y-a.y)*(b.y-a.y);
    return sqrt(xx+yy);
}
vector<vector<double> >Graph(vector<node_city> citylis,vector<link> linklis)
{
    vector<vector<double> > result;
    vector<double> temp;
    int n=citylis.size();
    for (int i=0;i<n;i++)  //构建矩阵
    {
        temp.push_back(9999999);
    }
    for (int i=0;i<n;i++)
    {
        result.push_back(temp);
    }
    for (int i=0;i<linklis.size();i++)  //读边信息
    {
        int citya=linklis[i].citya,cityb=linklis[i].cityb;
        result[citya][cityb]=result[cityb][citya]=getdistance(citylis[citya],citylis[cityb]);
    }
    for (int i=0;i<n;i++)  //将到自己与自己之间的距离记为0
    {
        result[i][i]=0;
    }
    return result;
}
vector<vector<int> > buildpath(vector<node_city> citylis) //建造原始路径记录数组
{
    vector<vector<int> >result;
    int n=citylis.size();
    vector<int> temp;
    for (int i=0;i<n;i++)
    {
        temp.push_back(i);
    }
    for (int i=0;i<n;i++)
    {
        result.push_back(temp);
    }
    return result;
}
floyd Getway(vector<vector<double> > Map,vector<vector<int> >Path,int citynum)
{
    floyd result;
    for (int u=0;u<citynum;u++)
    {
        for (int v=0;v<citynum;v++)
        {
            for (int w=0;w<citynum;w++)
            {
                if ((Map[v][u]+Map[u][w])<Map[v][w])
                {
                    Map[v][w]=Map[v][u]+Map[u][w];
                    Path[v][w]=Path[v][u];
                }
            }
        }
    }
    result.Graph=Map;
    result.Path=Path;
    return result;
}
int main()
{
    vector<node_city> citylis=getcity();
    vector<link> linklis=getlink();
    vector<vector<double> > Map=Graph(citylis,linklis);
    vector<vector<int> >path=buildpath(citylis);
    int citynum=citylis.size();
    floyd waypointer=Getway(Map,path,citynum);
    int x,y;
    cout<<"请输入导航出发地:"<<endl;
    cin>>x;
    cout<<"请输入导航目的地:"<<endl;
    cin>>y;
    cout<<"距离为:"<<fixed<<setprecision(4)<<waypointer.Graph[x][y]<<endl;
    int temp=waypointer.Path[x][y];
    //cout<<temp;
    cout<<"路径如下:"<<endl;
    cout<<x<<"-";
    while (temp!=y )
    {
        cout<<waypointer.Path[temp][y];
        temp=waypointer.Path[temp][y];
        if (temp!=y)
        {
            cout<<"-";
        }
    }
    return 0;
}

  2020-03-30 hyc写完作业上传。

测试数据格式:city.txt

0 293.3067 495.6110
1 534.5388 572.5732
2 668.2017 650.2673
3 130.6467 273.6333
4 627.1900 860.9881
5 821.0219 751.3575
6 126.3550 203.6050
7 936.0536 288.6235
8 570.0982 879.5848
9 868.7445 16.9076
10 525.6682 909.6910
11 280.9437 260.9239
12 318.1107 254.2589
13 30.8314 974.0659
14 546.2421 359.0124

link.txt

0 814
0 624
0 182
0 767
0 617
0 547
0 665
0 147
0 165
0 229
1 1
1 856
1 931
1 939
1 630
1 866
1 378
1 432

猜你喜欢

转载自www.cnblogs.com/Notherthing-hyc/p/12599789.html
今日推荐