【9931】火车票

Time Limit: 1 second
Memory Limit: 128 MB

【问题描述】

这里写图片描述

【输入格式】

【输出格式】

Sample Input

3 6 8 20 30 40
7
2 6
3
7
8
13
15
23

Sample Output

70

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=9931

【题解】

要注意只能从左往右走;
然后根据任意两个点之间的距离建边就可以了;
跑一下spfa;
我的f数组定义一开始写在int main函数里面;莫名的WA,然后写在主程序上面就没事?
以后都养成习惯写在int main上面吧。

【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

void rel(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void rei(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

const int MAXN = 100+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int l1,l2,l3,c1,c2,c3,n,s,t,dis[MAXN];
vector <int> g[MAXN],w[MAXN];
bool inque[MAXN];
queue <int> dl;
int f[MAXN];

void add(int x,int y,int z)
{
    g[x].pb(y);
    w[x].pb(z);
}

int main()
{
    //freopen("D:\rush.txt","r",stdin);
    cin >> l1 >> l2 >>l3>>c1>>c2>>c3;
    cin >>n;
    cin >> s >> t;
    if (s > t) swap(s,t);
    dis[1] = 0;
    rep1(i,2,n)
        rei(dis[i]);
    rep1(i,1,n-1)
        rep1(j,i+1,n)
            {
                int temp = abs(dis[i]-dis[j]);
                if (temp <=l1)
                    add(i,j,c1);
                else
                    if (temp<=l2)
                        add(i,j,c2);
                    else
                        if (temp <= l3)
                            add(i,j,c3);
            }
    memset(f,0x3f3f3f3f,sizeof f);
    f[s] = 0;
    inque[s] = true;
    dl.push(s);
    while (!dl.empty())
    {
        int x  = dl.front();
        inque[x] = false;
        dl.pop();
        int len = g[x].size();
        rep1(i,0,len-1)
        {
            int y = g[x][i],co = w[x][i];
            if (f[y]> f[x]+co)
            {
                f[y] = f[x]+co;
                if (!inque[y])
                {
                    inque[y] = true;
                    dl.push(y);
                }
            }
        }
    }
    cout << f[t]<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626856.html