Skip to content

测试样例补充:卡码网-47. 参加科学大会 #2992

Open

Description

@Aapolaris

本题提交如下代码,不论注释部分是否加上都能通过,但实际上在我的理解看来是必须加上的。

#include<iostream>
#include<vector>
#include<string.h>
#include<queue>
#include<climits>
using namespace std;

int edges[505][505];
bool visited[505];

int main()
{
    int n, m;
    cin >> n >>  m;
    memset(edges, 0, sizeof(edges));
    memset(visited, false, sizeof(visited));
    int s, t, val;
    while(m--){
        scanf("%d%d%d", &s, &t, &val);
        edges[s][t] = val;
    }

    vector<int>dist(n+1, INT_MAX);
    queue<int>q;
    q.push(1);
    dist[1] = 0;
    while(!q.empty()){
        int cur = q.front();    q.pop();
        visited[cur] = true;
        for(int t=1; t<=n; t++){
            if(edges[cur][t]==0)    continue;
            dist[t] = min(dist[t], dist[cur]+edges[cur][t]);
            if(!visited[t])  q.push(t);
        }
    }
    // for(int i=1; i<n; i++)
    //     if(edges[i][n]!=0){
    //         dist[n] = min(dist[n], dist[i]+edges[i][n]);
    //     }
    if(dist[n] == INT_MAX)  cout << -1;
    else    cout << dist[n];
}

举个例子:

4 4
1 2 2000
1 3 1
3 2 1
2 4 1

Activity

  1. Aapolaris commented on Jan 5, 2026

    @Aapolaris
    Author

    不对,这份代码,虽然ac了,但是不论加不加上注释部分都是不对的。比如下面这个例子:

    5 5
    1 2 2000
    1 3 1
    3 2 1
    2 4 1
    4 5 1
    

    还是题目提供的测试样例不足啊......

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions