AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

PAT自行车管理

作者: 作者的头像   Kue ,  2020-08-14 22:01:55 ,  所有人可见 ,  阅读 550


0


为啥我的代码在acwing有三个测试点错误?

..

//这里填你的代码^^
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int N = 510;
int g[N][N];
int bk[N], visit[N], d[N];
int bring = 0x3f3f3f, send = 0x3f3f3f;
vector<int> ans, path;
int m, n , c, S;
void dijkstrea()
{
    // 0 是起点
    memset(d , 0x3f, sizeof d);
    d[0] = 0;
    int s;
    for(int i = 0; i <= m; i++)
    {
        int MIN = 0x3f;
        for (int j = 0; j <= m; j++)
        {
            if (visit[j] == false && d[j] < MIN)
            {
                MIN = d[j];
                s = j;
            }
        }
        visit[s] = true;
        for (int j = 0; j <= m; j++)
        {
            if (d[j] > d[s] + g[s][j]) d[j] = d[s] + g[s][j];
        }
    }
}
// 直接减,就不需要额外的if else判断了
// 通过mins存储路径前往的过程, 有最大mins 需要take数量
void dfs(int u, int have, int mins)
{
    if (u)
    {
        // 求have
        have -= c / 2 - bk[u];
        mins = min (have, mins);
    }
    if (u == S)
    {
        int take = abs(mins);
        int back = take + have;

        if (take < send) ans = path , send = take , bring = back;
        else if (take == send && back < bring) ans = path, bring = back;
        return;
    }
    for(int i = 1; i <= m; i++)
    {
        if (d[i] == d[u] + g[u][i])
        {
            path.push_back(i);
            dfs(i, have, mins);
            path.pop_back();
        }
    }
}
int main()
{
    cin >> c >> m >> S >> n;
    memset(g , 0x3f, sizeof g);
    for (int i = 1; i <= m; i++) cin >> bk[i];

    for(int i = 0; i < n ; i++)
    {
        int a, b, dist;
        cin >> a >> b >> dist;
        g[a][b] = g[b][a] = dist;
    }

    dijkstrea();
    //cout << d[S] << endl;
    dfs(0, 0, 0);

    cout << send << " 0";
    for(int i = 0; i < ans.size(); i++) cout << "->" << ans[i];
    cout << " " << bring;
}
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

1 评论


用户头像
Kue   2020-08-14 22:49         踩      回复

MIN = 0x3f不能这样直接赋值, MIN会变成63


App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息