ccf 201712-4行车路线(100分)

问题描述

  小明和小芳出去乡村玩,小明负责开车,小芳来导航。
  小芳将可能的道路分为大道和小道。大道比较好走,每走1公里小明会增加1的疲劳度。小道不好走,如果连续走小道,小明的疲劳值会快速增加,连续走s公里小明会增加s2的疲劳度。
  例如:有5个路口,1号路口到2号路口为小道,2号路口到3号路口为小道,3号路口到4号路口为大道,4号路口到5号路口为小道,相邻路口之间的距离都是2公里。如果小明从1号路口到5号路口,则总疲劳值为(2+2)2+2+22=16+2+4=22。
  现在小芳拿到了地图,请帮助她规划一个开车的路线,使得按这个路线开车小明的疲劳度最小。

输入格式

  输入的第一行包含两个整数n, m,分别表示路口的数量和道路的数量。路口由1至n编号,小明需要开车从1号路口到n号路口。
  接下来m行描述道路,每行包含四个整数t, a, b, c,表示一条类型为t,连接a与b两个路口,长度为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,疲劳度为52=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 ≤ 105,1 ≤ a, b ≤ n,t是0或1,c ≤ 105。保证答案不超过106。

本题既有大路又有小路,行驶过程中无非4种情况 大路->大路 小路->小路 小路->大路 大路->小路。刚开始直接用迪杰斯特拉,虽然也得了100分,但后来想了想,如果某个点前驱是小路距原点的距离小于该点前驱是大路距原点的距离,但该点外围都是大路,那么该点前驱应该是大路。
例如:
5 6
1 1 2 3
1 2 3 2
0 1 3 30
0 3 4 20
0 4 5 30
1 3 5 6
应该输出66,但那个有bug的100分程序输出的是75。。。。。。。。。。
后来看了一些高手的程序,把小路和大路分开存储,再用Floyd算出各个点之间小路的最短距离,那么小路->小路就包含在内了,只需考虑三种情况,再用spfa算出各个情况下的最短距离,spfa和floyd需要熟练掌握。
正解的100分代码如下:

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const ll MAX_INF =0x3f3f3f3f;
ll dist0[505], dist1[505];//前驱分别是走大路和走小路的距离
ll g0[505][505], g1[505][505];//分别存储大路和小路的图
bool visit[505];
int n, m;
void floyd() //用floyd算法求出每两点之间小路的最短路
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            for (int k = 1; k <= n; k++)
            {
                if (i == k || j == k) continue;
                if (g1[i][j]>g1[i][k] + g1[k][j]) g1[i][j] = g1[j][i] = g1[i][k] + g1[k][j];
            }
        }
    }
}
void spfa()
{
    queue<ll> q;
    q.push(1);
    visit[1] = true;
    dist0[1] = 0, dist1[1] = 0;
    while (!q.empty())
    {
        ll front= q.front();
        q.pop();
        visit[front] = false;
        for (int i = 1; i <= n; i++)
        {

            if (g0[front][i]<MAX_INF&&dist1[front] + g0[front][i] < dist0[i])//小路->大路
            {
                dist0[i] = dist1[front] + g0[front][i];
                if (!visit[i]) //若不在队列则加入队列,以下同理
                {
                    q.push(i);
                    visit[i] = true;
                }
            }
            if (g0[front][i]<MAX_INF&&dist0[front] + g0[front][i] < dist0[i])//大路->大路
            {
                dist0[i] = dist0[front] + g0[front][i];
                if (!visit[i])
                {
                    q.push(i);
                    visit[i] = true;
                }
            }

                if (g1[front][i] <MAX_INF&&dist0[front] + g1[front][i] * g1[front][i] < dist1[i])//大路->小路
                {
                    dist1[i] = dist0[front] + g1[front][i] * g1[front][i];
                    if (!visit[i])
                    {
                        q.push(i);
                        visit[i] = true;
                    }
                }   
        }
    }
}
int main()
{
    memset(visit, false, sizeof(visit));
    memset(dist0, MAX_INF, sizeof(dist0));
    memset(dist1, MAX_INF, sizeof(dist1));
    memset(g0, MAX_INF, sizeof(g0));
    memset(g1, MAX_INF, sizeof(g1));
    cin >> n >> m;
    ll  t, a, b, c;
    for (ll i = 1; i <= m; i++)
    {
        cin >> t >> a >> b >> c;
        if (t == 0&&c<g0[a][b])
        {
            g0[a][b] = g0[b][a] = c; //大路
        }
        else if(t==1&&c<g1[a][b])
        {
            g1[a][b] = g1[b][a] = c; //小路
        }
    }
    floyd();
    spfa();
    ll ans = min(dist0[n], dist1[n]);
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinduo16/article/details/81628181