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];      //存储成本图
int d[MAXV];        //最短路径
int c[MAXV];        //最低成本
int pre[MAXV];        //记录前驱

void Dijkstra(int s)
{
  memset(visit,false,sizeof(visit));    //初始化标记
  for(int i=0 ; i<N ; ++i)        //初始化前驱数组
    pre[i]=i;
  fill(d,d+MAXV,INF);            //初始化最短距离为无穷大
  d[s]=0;                  //起始点到起始点距离为0
  fill(c,c+MAXV,INF);            //初始化最低成本为无穷大
  c[s]=0;                  //起始点到起始点成本为0

  for(int i=0 ; i<N ; ++i)
  {
    int MIN=INF,u=-1;
    for(int j=0 ; j<N ;++j)
    {
      if(d[j]<MIN && visit[j]==false)
      {
        MIN=d[j];
        u=j;
      }
    }
    if(u==-1)      //说明图不连通
      return;
    visit[u]=true;
    for(int v=0 ; v<N ; ++v)
    {
      if(visit[v]==false && GD[u][v]!=INF)    //v未被访问,且u可以到v
      {
        if(d[v]>d[u]+GD[u][v])          //距离不等,以最小距离更新
        {
          d[v]=d[u]+GD[u][v];
          c[v]=c[u]+GC[u][v];
          pre[v]=u;
        }
        else if(d[v]==d[u]+GD[u][v])      //距离相等,按成本最低更新
        {
          if(c[v]>c[u]+GC[u][v])
          {
            c[v]=c[u]+GC[u][v];
            pre[v]=u;
          }
        }
      }
    }
  }
}

void DFS(int s,int v)
{
  if(s==v)
  {
    printf("%d ",v);
    return;
  }
  DFS(s,pre[v]);
  printf("%d ",v);
}

int main()
{

  fill(GD[0],GD[0]+MAXV*MAXV,INF);
  fill(GC[0],GC[0]+MAXV*MAXV,INF);

  scanf("%d%d%d%d",&N,&M,&S,&D);
  for(int i=0 ; i<M ; ++i)          //存储城市距离和收费情况
  {
    int u,v;
    scanf("%d%d",&u,&v);
    scanf("%d%d",&GD[u][v],&GC[u][v]);
    GD[v][u]=GD[u][v];
    GC[v][u]=GC[u][v];
  }

  Dijkstra(S);
  DFS(S,D);
  printf("%d %d\n",d[D],c[D]);
  return 0;
}
时间: 2024-12-21 02:46:31

PAT:1030. Travel Plan (30) AC的相关文章

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 分)

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

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

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

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分)

最短路径问题.一定要注意将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:1080. Graduate Admission (30) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Student { int GE,GI,sum,rank,ID; int prefer[6]; }STU[40066]; struct School { int want; //各学校招生人数 int get; //已招到的人数 int line; //最后一名进来的排名线 int queue[4006

PAT:1004. Counting Leaves (30) AC

#include<stdio.h> #include<vector> const int MAX=510; using namespace std; int n,m,le=0; //节点数,非叶子节点数,最深层叶层数 vector<int> child[MAX]; //存储孩子情况 int number[MAX]; //每一层叶子数 void DFS(int s,int l) { if(child[s].size()==0) { ++number[l]; if(le&l

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