CCF之行车路线

问题描述

试题编号: 201712-4
试题名称: 行车路线
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  小明和小芳出去乡村玩,小明负责开车,小芳来导航。
  小芳将可能的道路分为大道和小道。大道比较好走,每走1公里小明会增加1的疲劳度。小道不好走,如果连续走小道,小明的疲劳值会快速增加,连续走 s公里小明会增加 s 2的疲劳度。
  例如:有5个路口,1号路口到2号路口为小道,2号路口到3号路口为小道,3号路口到4号路口为大道,4号路口到5号路口为小道,相邻路口之间的距离都是2公里。如果小明从1号路口到5号路口,则总疲劳值为(2+2) 2+2+2 2=16+2+4=22。
  现在小芳拿到了地图,请帮助她规划一个开车的路线,使得按这个路线开车小明的疲劳度最小。
输入格式
  输入的第一行包含两个整数 nm,分别表示路口的数量和道路的数量。路口由1至 n编号,小明需要开车从1号路口到 n号路口。
  接下来 m行描述道路,每行包含四个整数 tabc,表示一条类型为 t,连接 ab两个路口,长度为 c公里的双向道路。其中 t为0表示大道, t为1表示小道。保证1号路口和 n号路口是连通的。
输出格式
  输出一个整数,表示最优路线下小明的疲劳度。
样例输入
6 7
1 1 2 3
1 2 3 2
0 1 3 30
0 3 4 20
0 4 5 30
1 3 5 6
1 5 6 1
样例输出
76
样例说明
  从1走小道到2,再走小道到3,疲劳度为5 2=25;然后从3走大道经过4到达5,疲劳度为20+30=50;最后从5走小道到6,疲劳度为1。总共为76。
数据规模和约定
  对于30%的评测用例,1 ≤  n ≤ 8,1 ≤  m ≤ 10;
  对于另外20%的评测用例,不存在小道;
  对于另外20%的评测用例,所有的小道不相交;
  对于所有评测用例,1 ≤  n ≤ 500,1 ≤  m ≤ 10 5,1 ≤  ab ≤  nt是0或1, c  ≤ 10 5。保证答案不超过10 6


这道题目做了将近两个小时,最后得了80分,个人觉得还是有很多遗憾的。这个题实际上是对迪杰斯特拉算法的变形,下面把我的代码展示一下:

<textarea readonly="readonly" name="code" class="c++"> 
#include <iostream>
using namespace std;

int n,m;
static const int MAX=1010;
static const int INFTY=1e6;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

typedef struct Point
{
    int type;
    long long length;
    long long cost;
};

typedef struct Info
{
    int head;
};

Point G[MAX][MAX];
Info P[MAX];
int type[MAX];

void djs()
{
    int u;
    long long miniv;
    int d[MAX],color[MAX];//数组d是专门用来记录各条小道顶点的最短距离的
    long long cost[MAX];

    for(int i=0;i<n;i++)
    {
        cost[i]=INFTY;
        d[i]=INFTY;
        color[i]=WHITE;
    }

    d[0]=0;
    type[0]=1;
    cost[0]=0;
    color[0]=GRAY;
    while(1)
    {
        miniv=INFTY;
        u=-1;
        for(int i=0;i<n;i++)
        {
            if(miniv>cost[i]&&color[i]!=BLACK)
            {
                u=i;
                miniv=cost[i];
            }
        }
        if(u==-1) break;
        color[u]=BLACK;
        for(int v=0;v<n;v++)
        {
            if(color[v]!=BLACK&&G[u][v].length!=INFTY)
            {
                if(G[u][v].type==1) //小道
                {
                     if(type[u]==1)
                     {
                       if(d[v]>d[u]+G[u][v].length)
                       {
                         d[v]=d[u]+G[u][v].length;
                         if(d[v]*d[v]<cost[v])
                         {
                           cost[v]=d[v]*d[v];
                           color[v]=GRAY;
                           type[v]=1;
                         }
                       }
                     }
                    if(type[u]==0) //大道
                    {
                       d[v]=G[u][v].length;
                       if(cost[v]>cost[u]+d[v]*d[v])
                       {
                          cost[v]=cost[u]+d[v]*d[v];
                          color[v]=GRAY;
                       }

                    }
                }
                else  //大道
                {
                    if(cost[v]>cost[u]+G[u][v].length)
                    {
                      cost[v]=cost[u]+G[u][v].length;
                      color[v]=GRAY;
                      type[v]=0;
                    }

                }

            }
        }
    }
    cout<<cost[n-1]<<endl;
}

int main()
{
    int t,a,b;
    long long c;

    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
     for(int j=0;j<n;j++)
     {
        G[i][j].length=INFTY;
     }
    }
    for(int i=0;i<m;i++)
    {
        cin>>t>>a>>b>>c;
        G[a-1][b-1].length=G[b-1][a-1].length=c;
        G[a-1][b-1].type=G[b-1][a-1].type=t;
    }
    djs();
}
</textarea>

猜你喜欢

转载自blog.csdn.net/chengsilin666/article/details/79595964