PAT 1030 Travel Plan[图论][难]

1030 Travel Plan (30)(30 分)

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output

0 2 3 3 40

 题目大意:给出起点和终点,找出起点和终点之间的所有最短路径,并且cost最小。每条边有两个属性,一个是距离,一个是花费。

// 这个就是迪杰斯特拉裸题,我一定要做出来,见过多少次这样的题了。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 999999
int edge[500][500];
int cost[500][500];
int dist[500],co[500];
bool vis[500];
int pre[500];
vector<int> path;
int main() {
    int n,m,s,d;
    cin>>n>>m>>s>>d;
    int f,t,e,c;
    fill(cost[0],cost[0]+500*500,INF);//二维数组必须用cost[0],而不能用cost。
    fill(edge[0],edge[0]+500*500,INF);//这么多初始化有点烦。
    for(int i=0;i<m;i++){
        cin>>f>>t>>e>>c;
        edge[f][t]=edge[t][f]=e;
        cost[f][t]=cost[t][f]=c;
    }
    fill(dist,dist+n,INF);
    fill(co,co+n,INF);
    fill(pre,pre+n,0);
   for(int i=0;i<n;i++){
        if(edge[s][i]!=0)
            dist[i]=edge[s][i];//初始化
        if(cost[s][i]!=0)
            co[i]=cost[s][i];
   }
    //选出距离s最近的点。

    dist[s]=0;
    vis[s]=true;
    pre[s]=-1;
    int u=-1,minn=INF;
    for(int i=0;i<n;i++){
        u=-1,minn=INF;
        for(int j=0;j<n;j++){
            if(!vis[j]&&dist[j]<minn){
                minn=dist[j];
                u=j;
            }
        }
        if(u==-1||u==d)break;
        vis[u]=true;
        for(int j=0;j<n;j++){
            if(!vis[j]){
                if(dist[j]>dist[u]+edge[u][j]){
                    dist[j]=dist[u]+edge[u][j];
                    pre[j]=u;
                    co[j]=co[u]+cost[u][j];
                }else if(dist[j]==dist[u]+edge[u][j]){
                    if(co[j]>co[u]+cost[u][j]){
                        pre[j]=u;
                        co[j]=co[u]+cost[u][j];
                    }
                }
            }
        }
    }
    path.push_back(d);
    u=d;
    while(pre[u]!=-1){
        path.push_back(pre[u]);
        u=pre[u];
    }//s不用再单独push了。
    for(int i=path.size()-1;i>=0;i--){
        cout<<path[i]<<" ";
    }
    cout<<dist[d]<<" "<<co[d];

    return 0;
}

 

//第一次我是这么写的,但是提示内存超限。需要改进啊。原来考点在这个地方啊,又是考内存。内存超限:您的程序使用了超过限制的内存。case通过率为30.00%

都改成了vector加上resize,也不行,也是超时。

 //代码来自:https://www.liuchuo.net/archives/2369      十分值得学习,原来用dfs即可遍历。

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n, m, s, d;
int e[510][510], dis[510], cost[510][510];
vector<int> pre[510];//四个二维,一个一维
bool visit[510];
const int inf = 99999999;
vector<int> path, temppath;
int mincost = inf;
void dfs(int v) {//原来可以用向量来保存前驱,以前想到过但是不会实现。原来使用dfs.
    temppath.push_back(v);
    if(v == s) {
        int tempcost = 0;
        for(int i = temppath.size() - 1; i > 0; i--) {
            int id = temppath[i], nextid = temppath[i-1];
            tempcost += cost[id][nextid];
        }
        if(tempcost < mincost) {
            mincost = tempcost;
            path = temppath;//直接对一个向量赋值,即可。
        }
        temppath.pop_back();
        return ;
    }
    for(int i = 0; i < pre[v].size(); i++)
        dfs(pre[v][i]);
    temppath.pop_back();//最终把自己弹了出来。
}
int main() {
    fill(e[0], e[0] + 510 * 510, inf);
    fill(dis, dis + 510, inf);
    scanf("%d%d%d%d", &n, &m, &s, &d);
    for(int i = 0; i < m; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        scanf("%d", &e[a][b]);
        e[b][a] = e[a][b];
        scanf("%d", &cost[a][b]);
        cost[b][a] = cost[a][b];
    }
    pre[s].push_back(s);//s的前驱是s自己。
    dis[s] = 0;
    for(int i = 0; i < n; i++) {
        int u = -1, minn = inf;
        for(int j = 0; j < n; j++) {
            if(visit[j] == false && dis[j] < minn) {
                u = j;
                minn = dis[j];
            }
        }
        if(u == -1) break;
        visit[u] = true;
        for(int v = 0; v < n; v++) {
            if(visit[v] == false && e[u][v] != inf) {
                if(dis[v] > dis[u] + e[u][v]) {
                    dis[v] = dis[u] + e[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                } else if(dis[v] == dis[u] + e[u][v]) {
                    pre[v].push_back(u);
                }
            }
        }
    }
    dfs(d);
    for(int i = path.size() - 1; i >= 0; i--)
        printf("%d ", path[i]);
    printf("%d %d", dis[d], mincost);
    return 0;
}

 //我也不知道我改了什么,把自己的代码修好了。就最后循环出路径的while循环修改了,其他都没动,开心。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 99999
int edge[500][500];
int cost[500][500];
int dist[500],co[500];
bool vis[500];
int pre[500];
vector<int> path;
//两个二维矩阵,4个一维矩阵,一个向量。
int main() {
    int n,m,s,d;
    cin>>n>>m>>s>>d;
    int f,t,e,c;
    fill(cost[0],cost[0]+500*500,INF);//二维数组必须用cost[0],而不能用cost。
    fill(edge[0],edge[0]+500*500,INF);//这么多初始化有点烦。
    for(int i=0;i<m;i++){
        cin>>f>>t>>e>>c;
        edge[f][t]=edge[t][f]=e;
        cost[f][t]=cost[t][f]=c;
    }
    fill(dist,dist+n,INF);
    fill(co,co+n,INF);
    fill(pre,pre+n,-1);//要全初始化为-1
    //选出距离s最近的点。
    dist[s]=0;
    co[s]=0;
    int u,minn;
    for(int i=0;i<n;i++){
        u=-1,minn=INF;
        for(int j=0;j<n;j++){
            if(!vis[j]&&dist[j]<minn){
                minn=dist[j];
                u=j;
            }
        }
        if(u==-1||u==d)break;
        vis[u]=true;
        for(int j=0;j<n;j++){
            if(!vis[j]&&edge[u][j]!=INF){
                if(dist[j]>dist[u]+edge[u][j]){
                    dist[j]=dist[u]+edge[u][j];
                    pre[j]=u;
                    co[j]=co[u]+cost[u][j];
                }else if(dist[j]==dist[u]+edge[u][j]){
                    if(co[j]>co[u]+cost[u][j]){
                        pre[j]=u;
                        co[j]=co[u]+cost[u][j];
                    }
                }
            }
        }
    }
    u=d;
    while(u!=-1){
        path.push_back(u);
        u=pre[u];
    }//s不用再单独push了。
    for(int i=path.size()-1;i>=0;i--){
        cout<<path[i]<<" ";
    }
    cout<<dist[d]<<" "<<co[d];

    return 0;
}

 

posted @ 2018-08-09 20:13  lypbendlf  阅读(172)  评论(0编辑  收藏  举报