{"problem":{"name":"Come Back Quickly","description":{"content":"In the Republic of AtCoder, there are $N$ towns numbered $1$ through $N$ and $M$ roads numbered $1$ through $M$.   Road $i$ is a one-way road from Town $A_i$ to Town $B_i$, and it takes $C_i$ minutes ","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":3000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"abc191_e"},"statements":[{"statement_type":"Markdown","content":"In the Republic of AtCoder, there are $N$ towns numbered $1$ through $N$ and $M$ roads numbered $1$ through $M$.  \nRoad $i$ is a one-way road from Town $A_i$ to Town $B_i$, and it takes $C_i$ minutes to go through. It is possible that $A_i = B_i$, and there may be multiple roads connecting the same pair of towns.  \nTakahashi is thinking about taking a walk in the country. We will call a walk **valid** when it goes through one or more roads and returns to the town it starts at.  \nFor each town, determine whether there is a valid walk that starts at that town. Additionally, if the answer is yes, find the minimum time such a walk requires.\n\n## Constraints\n\n*   $1 \\le N \\le 2000$\n*   $1 \\le M \\le 2000$\n*   $1 \\le A_i \\le N$\n*   $1 \\le B_i \\le N$\n*   $1 \\le C_i \\le 10^5$\n*   All values in input are integers.\n\n## Input\n\nInput is given from Standard Input in the following format:\n\n$N$ $M$\n$A_1$ $B_1$ $C_1$\n$A_2$ $B_2$ $C_2$\n$A_3$ $B_3$ $C_3$\n$\\hspace{25pt} \\vdots$\n$A_M$ $B_M$ $C_M$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"abc191_e","tags":[],"sample_group":[["4 4\n1 2 5\n2 3 10\n3 1 15\n4 3 20","30\n30\n30\n-1\n\nBy Roads $1, 2, 3$, Towns $1, 2, 3$ forms a ring that takes $30$ minutes to go around.  \nFrom Town $4$, we can go to Towns $1, 2, 3$, but then we cannot return to Town $4$."],["4 6\n1 2 5\n1 3 10\n2 4 5\n3 4 10\n4 1 10\n1 1 10","10\n20\n30\n20\n\nThere may be a road such that $A_i = B_i$.  \nHere, we can use just Road $6$ to depart from Town $1$ and return to that town."],["4 7\n1 2 10\n2 3 30\n1 4 15\n3 4 25\n3 4 20\n4 3 20\n4 3 30","\\-1\n-1\n40\n40\n\nNote that there may be multiple roads connecting the same pair of towns."]],"created_at":"2026-03-03 11:01:13"}}