Submission #4249528


Source Code Expand

Copy
#include <bits/stdc++.h>
#include <tuple>
using namespace std;
using ll = long long;
const int INF = 1e9;
const int MOD = 1e9 + 7;
 
struct Node
{
  vector<int> to;
  vector<int> cost;
};
Node Graph[1010];
void AddEdge(int from, int to, int cost, Node *graph)
{
  graph[from].to.push_back(to);
  graph[from].cost.push_back(cost);
}
bool negative[1010];
ll dp[1010];
 
int main()
{
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i)
  {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    AddEdge(a, b, -c, Graph);
  }
 
  for (int i = 0; i < 1010; ++i)
  {
    dp[i] = 1e15;
  }
 
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
  for (int v = 0; v < n; ++v)
  {
    for (int k = 0; k < Graph[v].to.size(); ++k)
    {
      int to = Graph[v].to[k];
      ll cost = Graph[v].cost[k];
      if (dp[v] != 1e15 && dp[to] > dp[v] + cost)
      {
        dp[to] = dp[v] + cost;
      }
    }
  }
  }
  for (int v = 0; v < n; ++v)
  {
    for (int k = 0; k < Graph[v].to.size(); ++k)
    {
      int to = Graph[v].to[k];
      ll cost = Graph[v].cost[k];
      if (dp[to] > dp[v] + cost)
      {
        dp[to] = dp[v] + cost;
        negative[to] = true;
      }
    }
  }
  if (negative[n - 1])
  {
    cout << "inf" << endl;
    return 0;
  }
  cout << -dp[n - 1] << endl;
 
  return 0;
}

Submission Info

Submission Time
Task D - Score Attack
User commy
Language C++14 (GCC 5.4.1)
Score 400
Code Size 1370 Byte
Status
Exec Time 10 ms
Memory 384 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt, sample_03.txt
All 400 / 400 sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_2.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
Case Name Status Exec Time Memory
sample_01.txt 1 ms 256 KB
sample_02.txt 1 ms 256 KB
sample_03.txt 1 ms 256 KB
subtask_1_1.txt 2 ms 384 KB
subtask_1_10.txt 1 ms 256 KB
subtask_1_11.txt 2 ms 384 KB
subtask_1_12.txt 6 ms 384 KB
subtask_1_13.txt 3 ms 256 KB
subtask_1_14.txt 6 ms 384 KB
subtask_1_15.txt 10 ms 384 KB
subtask_1_16.txt 1 ms 256 KB
subtask_1_17.txt 1 ms 256 KB
subtask_1_18.txt 5 ms 384 KB
subtask_1_19.txt 9 ms 384 KB
subtask_1_2.txt 5 ms 384 KB
subtask_1_20.txt 3 ms 256 KB
subtask_1_21.txt 5 ms 384 KB
subtask_1_22.txt 10 ms 384 KB
subtask_1_23.txt 1 ms 256 KB
subtask_1_24.txt 7 ms 384 KB
subtask_1_25.txt 3 ms 384 KB
subtask_1_26.txt 6 ms 384 KB
subtask_1_27.txt 6 ms 384 KB
subtask_1_3.txt 4 ms 384 KB
subtask_1_4.txt 5 ms 384 KB
subtask_1_5.txt 3 ms 384 KB
subtask_1_6.txt 5 ms 384 KB
subtask_1_7.txt 4 ms 384 KB
subtask_1_8.txt 5 ms 384 KB
subtask_1_9.txt 1 ms 256 KB