Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- int n,m;
- struct ed{
- int fr, to, w;
- };
- vector <ed> G;
- const ll inf = 1e18;
- vector <ll> dst;
- vector <ll> check;
- void frd(){
- dst.resize(n, -inf);
- dst[0] = 0;
- for (int l = 0; l < n-1; ++l){
- // cout<<"l = "<<l<<"\n";
- for (int i = 0; i < m; ++i){
- if (dst[G[i].fr] == -inf) continue;
- if (dst[G[i].to] < dst[G[i].fr] + G[i].w){
- dst[G[i].to] = dst[G[i].fr] + G[i].w;
- }
- }
- }
- }
- void chk(){
- if (dst[n-1] == -inf){
- cout<<":(\n";
- exit(0);
- }
- check = dst;
- for (int l = 0; l < n-1; ++l){
- // cout<<"l = "<<l<<"\n";
- for (int i = 0; i < m; ++i){
- if (check[G[i].fr] == -inf) continue;
- if (check[G[i].to] < check[G[i].fr] + G[i].w){
- check[G[i].to] = check[G[i].fr] + G[i].w;
- }
- }
- }
- if (check[n-1] > dst[n-1]){
- cout<<":)\n";
- exit(0);
- }
- }
- void shG(){
- for (int i=0;i<m;++i){
- cout<<G[i].fr+1<<' '<<G[i].to+1<<' '<<G[i].w<<"\n";
- }
- }
- int main()
- {//отдебажь на тесте, который у тебя есть!!!
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin>>n>>m;
- G.resize(m);
- for (int i = 0; i < m;++i) {
- cin>>G[i].fr>>G[i].to>>G[i].w;
- G[i].fr--;
- G[i].to--;
- }
- //shG();
- frd();
- chk();
- cout<<dst[n-1]<<"\n";
- //cout<<dst[n-1]<<"\n";
- }
- /*
- 2 2
- 1 2 3
- 1 2 7
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement