1030 Travel Plan (30 分)(最短路径 and dfs)

#include<bits/stdc++.h>

using namespace std;
const int N=510;
const int inf=0x3f3f3f3f;
int mp[N][N];
bool vis[N];
int dis[N];
int n,m,s,D;
int cost[N][N];
vector<int>path[N];
void Dijkstra()
{
    fill(vis,vis+N,false);
    fill(dis,dis+N,inf);
    for(int i=0;i<n;i++) dis[i]=mp[s][i];
    dis[s]=0;
    for(int i=0;i<n-1;i++){
        int u=-1;
        int minn=inf;
        for(int j=0;j<n;j++){
            if(!vis[j]&&dis[j]<minn){
                u=j;
                minn=dis[j];
            }
        }
        if(u==-1) return;
        vis[u]=true;
        for(int j=0;j<n;j++){
            if(!vis[j]&&dis[u]+mp[u][j]<=dis[j]){
                if(mp[u][j]+dis[u]<dis[j]){
                    dis[j]=mp[u][j]+dis[u];
                    path[j].clear();
                    path[j].push_back(u);
                }
                else{
                    path[j].push_back(u);
                }
            }
        }

    }
}
vector<int>t,p;
int minn=inf;
void dfs(int v)
{

    if(v==s){
        t.push_back(v);
        int sum=0;
        for(int i=t.size()-1;i>0;i--){
            int a=t[i];
            int b=t[i-1];
            sum+=cost[a][b];
        }
        if(sum<minn){
            minn=sum;
            p=t;
        }
        t.pop_back();
        return;
    }
    t.push_back(v);
    for(int i=0;i<path[v].size();i++){
        dfs(path[v][i]);
    }
    t.pop_back();

}
int main()
{
    fill(cost[0],cost[0]+N*N,0);
    fill(mp[0],mp[0]+N*N,inf);
    scanf("%d %d %d %d",&n,&m,&s,&D);
    while(m--){
        int a,b,c,d;
        scanf("%d %d %d %d",&a,&b,&c,&d);
        mp[a][b]=mp[b][a]=c;
        cost[a][b]=cost[b][a]=d;
    }
    Dijkstra();
    dfs(D);
    for(int i=p.size()-1;i>=0;i--){
        printf("%d ",p[i]);
    }
    printf("%d %d\n",dis[D],minn);
    return 0;
}

原文地址:https://www.cnblogs.com/chenchen-12/p/10127464.html

时间: 2024-10-09 17:16:30

1030 Travel Plan (30 分)(最短路径 and dfs)的相关文章

1030 Travel Plan (30 分)

1030 Travel Plan (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 ci

1030 Travel Plan (30分)

最短路径问题.一定要注意将visited[u]是否等于0加入判断!很关键,还是没有理解透,所以才忘了加.(第42行和第45行) 其实图相关的题通过率蛮高的...套路题,坑少. 1 #include <iostream> 2 #include<cstdio> 3 #include<vector> 4 #define inf 0x3f3f3f3f 5 using namespace std; 6 struct Edge{ 7 int next,dis,cos; 8 }; 9

PAT 1030. Travel Plan (30)

1030. Travel Plan (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 cit

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.

1030. Travel Plan (30)

dfs使用vector保存最短路径 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 tr

PAT:1030. Travel Plan (30) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXV=510; const int INF=0x3fffffff; int N,M,S,D; //城市数,高速公路数,起点,终点 bool visit[MAXV]; //标记在迪杰斯特拉中是否遍历过 int GD[MAXV][MAXV]; //存储距离图 int GC[MAXV][MAXV];

A1030 Travel Plan (30分)

一.技术总结 这一题是关于图的遍历,但是涉及Djikstra算法,在求最短路径的同时,还要把路径记录下来:同时增加了边权,也就会每个城市之间的花费: 这里采用Djikstra算法+DFS遍历的方法 第一步使用Djikstra算法求出最短路径,使用vector类型pre数组进行存储,每个结点里面保存的是他们的前驱结点. 然后再使用DFS遍历pre,然后会有得出遍历树. 要注意下标是0~n-1,还是1~n. 细节问题参考代码 二.参考代码 #include<bits/stdc++.h> using

PAT (Advanced Level) 1030. Travel Plan (30)

先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int INF=0x7FFFFFFF; const int

pat1030. Travel Plan (30)

1030. Travel Plan (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

1030 Travel Plan (30 分)

1030 Travel Plan (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 ci