【*】第三题:收费站(提高组第一试2011年10月21日)(2011年NOIP冲刺模拟试题)(

来源:互联网 发布:python爬虫赚钱收入 编辑:程序博客网 时间:2024/04/30 18:37

 收费站

【题目描述】

    在某个遥远的国家里,有n个城市。编号为1,2,3,……,n。

    这个国家的政府修建了m条双向的公路。每条公路连接着两个城市。沿着某条公路,开车从一个城市到另一个城市,需要花费一定的汽油。

    开车每经过一个城市,都会被收取一定的费用(包括起点和终点城市)。所有的收费站都在城市中,在城市间的公路上没有任何的收费站。

    小红现在要开车从城市u到城市v(1<=u,v<=n)。她的车最多可以装下s升的汽油。在出发的时候,车的油箱是满的,并且她在路上不想加油。

    在路上,每经过一个城市,她要交一定的费用。如果她某次交的费用比较多,她的心情就会变得很糟。所以她想知道,在她能到达目的地的前提下,她交的费用中最多的一次最少是多少。这个问题对于她来说太难了,于是她找到了聪明的你,你能帮帮她吗?

 

【输入】

    第一行5个正整数,n,m,u,v,s。分别表示有n个城市,m条公路,从城市u到城市v,车的油箱的容量为s升。

    接下来有n行,每行1个正整数,fi。表示经过城市i,需要交费fi元。

    再接下来有m行,每行3个正整数,ai,bi,ci(1<=ai,bi<=n)。表示城市ai和城市bi之间有一条公路,如果从城市ai到城市bi,或者从城市bi到城市ai,需要用ci升汽油。

 

【输出】

    仅一个整数,表示小红交费最多的一次的最小值。

    如果她无法到达城市v,输出-1。

 

【样例输入输出1】

cost.in

cost.out

4 4 2 3 8

8

5

6

10

2 1 2

2 4 1

1 3 4

3 4 3

8

 

【样例输入输出2】

cost.in

cost.out

4 4 2 3 3

8

5

6

10

2 1 2

2 4 1

1 3 4

3 4 3

-1

 

【数据规模】

    对于60%的数据,满足n<=200,m<=10000,s<=200

    对于100%的数据,满足n<=10000,m<=50000,s<=1000000000

    对于100%的数据,满足ci<=1000000000,fi<=1000000000,可能有两条边连接着相同的城市。

 

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;FILE *in, *out;typedef long long int ll;ll q[3000001];ll f[10001];ll a[10001];ll d[10001];ll v[10001];ll b[10001][251];ll c[10001][251];ll n, m, st, ed, tt, limit, tot;void find_it(ll s, ll t){    ll i, j, x, tmp;    i = s;    j = t;    x = f[(i + j) / 2];    while (i <= j) {        while (f[i] < x) { ++i; }        while (f[j] > x) { --j; }        if (i <= j) {                tmp = f[i];                f[i] = f[j];                f[j] = tmp;                ++i;                --j;        }    }    if (s < j) find_it(s, j);    if (i < t) find_it(i, t);        }void spfa(ll s){    ll i, head, tail, now;    sizeof(v, 0, sizeof(v));    for (i = 1; i <= n; ++i) d[i] = 0x7fffffff;    d[s] = 0;    q[1] = s;    head = 1;    tail = 1;    v[s] = 1;    while (head <= tail) {        now = q[head];        for (i = 1; i <= b[now][0]; ++i) {                if (a[b[now][i]]<=limit) {                    if (d[b[now][i]]>d[now]+c[now][i]) {                            d[b[now][i]] = d[now]+c[now][i];                            if (v[b[now][i]] == 0) {                                v[b[now][i]] = 1;                                ++tail;                                q[tail] = b[now][i];                            }                    }                }        }        ++head;        v[now] = 0;    }}void process(ll s, ll t){    ll mid;    if (s == t) { fprintf(out, "%d\n", f[s]); fclose(in);fclose(out);return ;}    mid = (s + t) / 2;    limit = f[mid];    spfa(st);    if (d[ed] > tt) process(mid + 1, t);    else process(s, mid);}int main(){    in = fopen("cost.in", "rt");    out = fopen("cost.out", "wt");    ll s, t, l, i, lim;    fscanf(in, "%lld%lld%lld%lld%lld", &n, &m, &st, &ed, &tt);    for (i = 1; i <= n; ++i) fscanf(in, "%lld", &a[i]);    if (a[st] > a[ed]) lim = a[st];    else lim = a[ed];    for (i = 1; i <= n; ++i) {        if (a[i] >= lim) {                ++tot;                f[tot] = a[i];        }    }    find_it(1, tot);    for (i = 1; i <= m; ++i) {        fscanf(in, "%lld%lld%lld", &s, &t, &l);        ++b[s][0];        b[s][b[s][0]] = t;        c[s][b[s][0]] = l;        ++b[t][0];        b[t][b[t][0]] = s;        c[t][b[t][0]] = l;    }    limit = 1000000001;    spfa(st);        if (d[ed] > tt)  { fprintf(out, "-1\n"); system("pause");fclose(in);fclose(out);return 0;}    process(1, tot);    system("pause");    return 0;}/*4 4 2 3 88 5 6 102 1 22 4 11 3 43 4 34 4 2 3 38 5 6 102 1 22 4 11 3 43 4 3*/

原创粉丝点击