1111 Online Map(30 分)

有一个测试点未通过,写老长了,累死

#include
#include
#include
#include
#include
using namespace std;
#define STP system("pause")
#pragma warning(disable:4996)
#define inf 99999999
#define OT(x) {copy(x.begin(), x.end()-1, ostream_iterator(std::cout, " -> "));\
                cout << x.back();}

const int Size = 10;

int **vetex, **tim, **dis,**w_dis;//定义三个指向二维数组的指针
int M, N, a, b, c, d, e, src, des;
int depth = 1;
vector visit;//是否已经访问过

int cnt = 0;
int alldep = 0;
int dfs(int inp,int dep)//开始的顶点
{
    if (inp==des)
    {
        cout << inp << endl;//inp是终点
        //cout << dep << endl;//dep深度大小
        //cout << cnt<< endl;//cnt是每次回退的次数

        alldep = dep;
        cnt = 0;
        return ++cnt;
    }
    for (int i = 0; i  ";
            visit[inp] = true;
            int ta=dfs(i,1+dep);
            visit[inp] = false;
        }
    }
    return ++cnt;
}

int main()
{

    cin >> M >> N;//M:地点数目,N:街道数目

    vetex = new int*[M] {};
    w_dis = new int*[M] {};
    tim = new int*[M] {};
    dis = new int*[M] {};
    visit.resize(M);
    for (int i = 0; i < M; i++)
    {
        vetex[i] = new int[M] {};
        w_dis[i] = new int[M] {};
        dis[i] = new int[M] {};
        tim[i] = new int[M] {};
        visit[i] = false;
    }
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < M; j++)
        {
            vetex[i][j] = 0;
            w_dis[i][j] = inf;
            tim[i][j] = inf;
            dis[i][j] = inf;
        }
    }





    //数组的初始化操作
    /*for (int i = 0; i < M; i++)  
    {
        for (int j = 0; j < M; j++)
        {
            cout << vetex[i][j] << " ";
        }
        cout << endl;
    }*/
    

    for (int i = 0; i < N; i++)
    {
        cin >> a >> b >> c >> d >> e;
        if (c==1)//单向的街道
        {
            vetex[a][b] = 1;
            dis[a][b] = d;//距离的赋值
            tim[a][b] = e;//时间的赋值
        }
        else//双向的街道
        {
            vetex[a][b] = vetex[b][a] = 1;
            dis[a][b] = dis[b][a] = d;//距离的赋值
            tim[a][b] = tim[b][a] = e;//时间的赋值
        }
    }
    cin >> src >> des;






    //矩阵连接情况统计
    /*cout << "--------------------------\n";
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < M; j++)
        {
            cout << vetex[i][j] << " ";
        }
        cout << endl;
    }
    cout << "--------------------------\n";*/





    //深度优先搜索
    //dfs(src, depth);





    /*这一部分是局部的最优解,也就是深度优先搜索,写错了,dijstra需要使用bfs算法
    int sts = src,sts1=src;
    vector v_P;//前驱结点的集合
    while (sts != des)
    {
        int mindis = inf;//设定初始值
        int les_sta = -1;
        for (int i = 0; i < M; i++)
        {
            if (vetex[sts][i]==1&&visit[i]==false)//如果可以通行并且之前未访问过则继续执行
            {
                v_P.push_back(sts);//保存已经访问过的点
                w_dis[sts][i] = dis[sts][i];//若大于则取值,保存小值
                if (mindis>dis[sts][i])
                {
                    mindis = dis[sts][i];
                    les_sta = i;//计算所有临近点中距离最小的点
                }
            }
        }
        visit[sts] = true;//已经访问过
        cout << sts << "———>";
        sts = les_sta;
        for (int i = 0; i < v_P.size(); i++)
        {
            if (w_dis[v_P[i]][les_sta] > w_dis[v_P[i]][sts] + dis[sts][les_sta]&&visit[les_sta]==false)//如果未访问过的点更新之后的和小于原先的和则再次更新
                {
                    w_dis[v_P[i]][les_sta] = w_dis[v_P[i]][sts] + dis[sts][les_sta];
                }
        }
    } 
    cout << w_dis[3][5] << endl;*/





    int k,tmp;//起始顶点
    vectorpre(M);//前驱结点
    vectordist(M);//长度数组
    vectordist_time(M);//走到该点所用时间的数组


    for (int i = 0; i < M; i++)//初始化操作
    {
        visit[i] = false;
        pre[i] = 0;
        dist[i] = dis[src][i];
        dist_time[i] = tim[src][i];
    }
    //OT(dist);

    visit[src] = true;
    dist[src] = 0;

    for (int i = 1; i < M; i++)
    {
        int mindis = inf;
        for (int j = 0; j < M; j++)//找到顶点中最邻近的一个点
        {
            if (visit[j] == false && dist[j] < mindis)//未曾访问过并且可通过
            {
                mindis = dist[j];
                k = j;
            }
        }
        //cout << k;
        visit[k] = true;//标记最邻近的节点k已经找到了并访问过

        for (int j = 0; j < M; j++)
        {
            tmp = (dis[k][j] == inf)? inf:(mindis + dis[k][j]);
            if ((visit[j]==false)&&(tmp<=dist[j]))//如果原值比新找到的路径大就更新
            {
                if (tmpdist_time[k]+tim[k][j])//如果原先的路径耗时较大则更新
                    {
                        dist[j] = tmp;
                        pre[j] = k;
                        dist_time[j] = dist_time[k] + tim[k][j];
                    }
                
            }
        }
    }

    



    int u, tem1;
    vector pretim(M);
    vector timless(M);
    vector timl_dis(M);


    for (int i = 0; i < M; i++)
    {
        pretim[i] = 0;
        timless[i] = tim[src][i];
        visit[i] = false;
        timl_dis[i] = dis[src][i];
    }

    visit[src] = true;
    timless[src] = 0;

    for (int i = 1; i < M; i++)
    {
        int mintim = inf;
        for (int j = 0; j < M; j++)
        {
            if (visit[j]==false&&timless[j] timl_dis[u] + dis[u][k])//原先的大于现在的
                {
                    timless[k] = tem1;
                    pretim[k] = u;
                    timl_dis[k] = timl_dis[u] + dis[u][k];
                }
                
            }
        }
    }

    vectorv_outdis{ des };
    vectorv_outtim{ des };

    for (int i = des; pre[i] != 0;)
    {
        v_outdis.push_back(pre[i]);
        i = pre[i];
    }
    v_outdis.push_back(src);
    reverse(v_outdis.begin(), v_outdis.end());

    for (int i = des; pretim[i] != 0;)
    {
        v_outtim.push_back(pretim[i]);
        i = pretim[i];
    }
    v_outtim.push_back(src);
    reverse(v_outtim.begin(), v_outtim.end());

    if (v_outtim == v_outdis)
    {
        cout << "Distance = " << dist[des] << "; ";
        cout << "Time = " << timless[des] << ": ";
        OT(v_outdis);
        cout << endl;
    }
    else
    {
        cout << "Distance = " << dist[des] << ": ";
        OT(v_outdis);
        cout << endl;

        cout << "Time = " << timless[des] << ": ";
        OT(v_outtim);
        cout << endl;
    }

    /*数组的删除操作*/

    for (int i = 0; i < M; i++)
    {
        delete[]  vetex[i];
        delete[]  tim[i];
        delete[]  dis[i];
        delete[]  w_dis[i];
    }
    delete[]vetex;
    delete[]tim;
    delete[]dis;
    delete[]w_dis;
    STP;
    return 0;
}

你可能感兴趣的:(1111 Online Map(30 分))