結果

問題 No.2712 Play more!
ユーザー rotti_coderrotti_coder
提出日時 2024-03-31 14:40:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,941 bytes
コンパイル時間 1,280 ms
コンパイル使用メモリ 97,804 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-31 14:40:18
合計ジャッジ時間 2,616 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 WA -
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 35 ms
6,676 KB
testcase_08 AC 35 ms
6,676 KB
testcase_09 AC 36 ms
6,676 KB
testcase_10 WA -
testcase_11 AC 12 ms
6,676 KB
testcase_12 AC 53 ms
6,676 KB
testcase_13 AC 13 ms
6,676 KB
testcase_14 AC 47 ms
6,676 KB
testcase_15 AC 46 ms
6,676 KB
testcase_16 AC 8 ms
6,676 KB
testcase_17 AC 5 ms
6,676 KB
testcase_18 AC 7 ms
6,676 KB
testcase_19 AC 5 ms
6,676 KB
testcase_20 AC 21 ms
6,676 KB
testcase_21 AC 13 ms
6,676 KB
testcase_22 AC 25 ms
6,676 KB
testcase_23 AC 12 ms
6,676 KB
testcase_24 AC 9 ms
6,676 KB
testcase_25 AC 6 ms
6,676 KB
testcase_26 AC 8 ms
6,676 KB
testcase_27 AC 9 ms
6,676 KB
testcase_28 AC 8 ms
6,676 KB
testcase_29 AC 10 ms
6,676 KB
testcase_30 AC 75 ms
6,676 KB
testcase_31 AC 10 ms
6,676 KB
testcase_32 AC 9 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<set>
using namespace std;
vector<int> bfs(int n,int m,vector<vector<int>>rotti,int start){
  vector<int>stack{start};
  int iti=0,hako;
  set<int>irane{start};
  while (stack.size()>iti){
    hako=stack[iti];
    for (int i=0;i<rotti[hako-1].size();i++){
      if (irane.count(rotti[hako-1][i])>0)continue;
      irane.insert(rotti[hako-1][i]);
      stack.emplace_back(rotti[hako-1][i]);
    }
    iti++;
  }
  vector<int>ans(n,0);
  //連結してるなら1
  for (int i=0;i<n;i++)if(irane.count(i+1)>0)ans[i]=1;
  return ans;
}
vector<long long> dijkstra_negative(int n,int m,vector<vector<vector<long long>>>graph,int start,vector<int>num){
  //連結じゃない頂点には手をださない
  //vector<int>numの中身はそれぞれの頂点がスタート地点から連結かどうか(連結なら1)
  vector<long long>amount(n,1e18);
  int check;
  amount[start-1]=0;
  for (int q=0;q<n+1;q++){
    check=0;
    for (int i=0;i<n;i++){
      if(num[i]==0)continue;
      for (int ipp=0;ipp<graph[i].size();ipp++){
        if(amount[graph[i][ipp][0]-1]<=amount[i]+graph[i][ipp][1])continue;
        check=1;
        amount[graph[i][ipp][0]-1]=amount[i]+graph[i][ipp][1];
      }
    }
    if(check==0)break;
  }
  //負の経路が存在する
  if(check==1){
    amount[start-1]=-1;
    return amount;
  }
  return amount;
}
int main()
{
  int n,m;
  cin >> n >> m;
  vector<long long>A(n);
  vector<vector<vector<long long>>>graph(n);
  vector<vector<int>>graph2(n);
  for(int i=0;i<n;i++)cin >> A[i];
  for(int i=0;i<m;i++){
    int a,b,c;
    cin >> a >> b >> c;
    graph[a-1].push_back({b,c-A[b-1]});
    graph2[a-1].emplace_back(b);
  }
  vector<int>num=bfs(n,m,graph2,1);
  vector<long long>amount=dijkstra_negative(n,m,graph,1,num);
  long long hako=-1;
  if(amount[0]==-1 || num[n-1]==0)cout << "inf" << endl;
  else cout << amount[n-1]*hako+(long long)A[0] << endl;
}
0