文章

7

粉丝

388

获赞

28

访问

77.5k

头像

本题数据疑似有误,具体说明在题解中(另附dp/图论两种解法)

P1173 清华大学上机题
发布于 2021年1月
阅读数 11.6k

疑似是造的数据未考虑到会爆精度的问题,测试的样例输出超过了int精度,这导致以下代码会被判为AC

#include <cstdio>
#include <climits>
typedef long long ll;
const int maxn = 510;
const int INF = 0x7fffffff;
int dist[maxn], cost[maxn];//第i个站的总里程、最少花费
int l1, l2, l3, c1, c2, c3;
 
int Price(int L) {//L距离的票多少钱
    if(L<=l1) return c1;
    else if(L<=l2) return c2;
    else return c3;
}
int n, from, to;
int main() {
    
    while(scanf("%d%d%d",&l1,&l2,&l3)!=EOF) {
        scanf("%d%d%d",&c1,&c2,&c3);
        dist[1]=0;//始发站里程为0
        scanf("%d%d", &from, &to);
        scanf("%d", &n);
        for(int i=2; i<=n; i++) scanf("%d", &dist[i]);
        cost[from]=0;//出发之前,没花一毛钱
        for(int i=from+1; i<=to; i++) {//前进!!!
            cost[i]= INF;//先假设到i站需要花无数的钱
            for(int j=from; j<i; j++) {//到i站的票可能是从j站买的
                int L=dist[i]-dist[j];//j站到i站的距离
                if(L<=l3&&cost[j]+Price(L)<cost[i])
                    cost[i]=cost[j]+Price(L);
                //如果从j站买票能比以往的方案更省钱,那就从j买票
            }
        }
        printf("%d\n", cost[to]);
    }
}

然而只要将所有的条件改为long long则会判WA(0.33分)

#include <cstdio>
#include <climits>
typedef long long ll;
const int maxn = 510;
const ll INF = 1145141919810ll;
ll dist[maxn], cost[maxn];//第i个站的总里程、最少花费
ll l1, l2, l3, c1, c2, c3;
 
ll Price(ll L) {//L距离的票多少钱
    if(L<=l1) return c1;
    else if(L<=l2) return c2;
    else return c3;
}
int n, from, to;
int main() {
    
    while(scanf("%lld%lld%lld",&l1,&l2,&l3)!=EOF) {
        scanf("%lld%lld%lld",&c1,&c2,&c3);
        dist[1]=0;//始发站里程为0
        scanf("%d%d", &from, &to);
        scanf("%d", &n);
        for(int i=2; i<=n; i++) scanf("%lld", &dist[i]);
        cost[from]=0;//出发之前,没花一毛钱
        for(int i=from+1; i<=to; i++) {//前进!!!
            cost[i]= INF;//先假设到i站需要花无数的钱
            for(int j=from; j<i; j++) {//到i站的票可能是从j站买的
                ll L=dist[i]-dist[j];//j站到i站的距离
                if(L<=l3&&cost[j]+Price(L)<cost[i])
                    cost[i]=cost[j]+Price(L);
                //如果从j站买票能比以往的方案更省钱,那就从j买票
            }
        }
        printf("%lld\n", cost[to]);
    }
}

很明显,两者的差距仅在数据类型上,这很有可能是本oj该题的样例输出未考虑到答案可能会超出int表示范围所导致的

顺便附带一个图论最短路做法,由于数据范围是long long,在本OJ提交依旧只有0.33分

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
typedef long long ll;
const int maxn = 10010;
const int maxm = 1000010;
const ll INF = 1145141919810114514ll;
using namespace std;
ll L1, L2, L3;//分段距离
ll C1, C2, C3;//分段费用
ll price(ll length) {
    if(length <= L1) return C1;
    if(L1 < length && length <= L2) return C2;
    else return C3;
}
struct info {
    int no;
    int length;
    bool operator < (const info & o) const {
        if(length == o.length) return no < o.no;
        else return length < o.length;
    }
};
ll a[maxn];
int s, t;
int n;
struct Edge {
    int v, next;
    ll w;
}edges[maxm << 1];
int head[maxn], cnt;
ll dis[maxn];
bool vis[maxn];
void add_edge(int u, int v, ll w) {
    edges[++cnt].v = v;
    edges[cnt].w = w;
    edges[cnt].next = head[u];
    head[u] = cnt;
}
void init() {
    memset(head, 0, sizeof(head));
    memset(vis, 0, sizeof(vis));
    cnt = 0;
    for(int i = s; i <= t; ++i) dis[i] = INF;
}
struct node {
    int v; ll w;
    node(int _v = 0, ll _w = 0) {v = _v, w = _w;}
    bool operator < (const node & o) const {
        return o.w < w;
    }
};
priority_queue<node> q;
void dijkstra(int s, ll* d) {
    while(!q.empty()) q.pop();
    memset(vis, 0, sizeof(vis));
    d[s] = 0;
    q.push(node(s, 0));
    while(!q.empty()) {
        node tmp = q.top(); q.pop();
        int u = tmp.v;
        if(vis[u])continue;
        vis[u] = true;
        for(int i = head[u]; i; i = edges[i].next) {
            int v = edges[i].v; ll w = edges[i].w;
            if(!vis[v] && d[v] > d[u] + w) {
                d[v] = d[u] + w;
                q.push(node(v, d[v]));
            }
        }
    }
}
int main() {
    while (scanf("%lld%lld%lld", &L1, &L2, &L3) != EOF) {
        scanf("%lld%lld%lld", &C1, &C2, &C3);
        scanf("%d%d", &s, &t);
        if(s > t) swap(s, t);
        scanf("%d", &n);
        for(int i = 2; i <= n; ++i) scanf("%lld", &a[i]);
        init();
        for(int i = 1; i < n; ++i) {
            int cnt = i + 1;
            while(cnt <= t && a[cnt] - a[i] <= L3)
                add_edge(i, cnt, price(a[cnt] - a[i])), ++cnt;
        }
        dijkstra(s, dis);
        printf("%lld\n", dis[t]);
    }
}

 



登录后发布评论

暂无评论,来抢沙发