首页 > 代码库 > ABC061

ABC061

D - Score Attack(最长路 + 判环)

Problem Statement

There is a directed graph with N vertices and M edges. The i-th edge (1≤iM) points from vertex a**i to vertex b**i, and has a weight c**i. We will play the following single-player game using this graph and a piece.

Initially, the piece is placed at vertex 1, and the score of the player is set to 0. The player can move the piece as follows:

  • When the piece is placed at vertex a**i, move the piece along the i-th edge to vertex b**i. After this move, the score of the player is increased by c**i.

The player can end the game only when the piece is placed at vertex N. The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.

When the player acts optimally to maximize the score at the end of the game, what will the score be? If it is possible to increase the score indefinitely, print inf.

Constraints

  • 2≤N≤1000

  • 1≤Mmin(N(N−1),2000)

  • 1≤a**i,b**iN(1≤iM)

  • a**ib**i(1≤iM)

  • a**ia**j or b**ib**j(1≤i<jM)

  • −109≤c**i≤109(1≤iM)

  • c**i is an integer.

  • In the given graph, there exists a path from vertex 1 to vertex N.

Input

Input is given from Standard Input in the following format:

N M  a1 b1 c1  a2 b2 c2:  aM bM cM  

Output

Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.

Sample Input 1

3 31 2 42 3 31 3 5

Sample Output 1

7

技术分享

Code:

技术分享
 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 static const int MAXN = 1e4 + 10; 5 static const LL OO = (long long)1 << 60; 6 int n , m; 7 struct Node 8 { 9     int x , y;10 };11 int q[MAXN * MAXN];12 bool vis[MAXN];13 int times[MAXN];14 vector<Node> data[MAXN];15 int pre , tail;16 bool flag;17 LL dis[MAXN];18 LL mx;19 bool first = 1;20 void Spfa()21 {22     q[tail++] = 1;23     times[1] = 1;dis[1] = 0;24     while(pre < tail)25     {26         int u = q[pre++];27 28         if(times[u] > n)29         {30             flag = 1;31             return ;32         }33         for(auto nxt:data[u])34         {35             int v = nxt.x;36             int co = nxt.y;37 38             if(dis[v] < dis[u] + co)39             {40                 dis[v] = dis[u] + co;41                 ++times[v];42                 if(!vis[v])43                 {44                     if(v == n && first)///!!!!!!!!45                     {46                         mx = dis[n] , first = 0;47                     }48                     vis[v] = 1;49                     q[tail++] = v;50                 }51             }52         }53         vis[u] = 0;54     }55 }56 57 int main()58 {59     pre = tail = 0;60     scanf("%d%d" , &n , &m);61     for(int i = 0 ; i <= n ; ++i)62     {63         dis[i] = -OO;64     }65     for(int i = 1 ; i <= m ; ++i)66     {67         int a , b , c;68         scanf("%d%d%d" , &a , &b , &c);69         data[a].push_back({b , c});70     }71     Spfa();72 73     if(flag)74     {75         if(dis[n] == mx) ///!!!!!!!!76             printf("%lld" , mx);77         else78             puts("inf");79     }80     else81     {82         printf("%lld\n" , dis[n]);83     }84 85 }
View Code

 

ABC061