NegativeGraphDiv1
SRM 626 · 2013-12-22 · by lg5293
Problem Statement
Nancy has a directed graph with N vertices and E edges.
The vertices are numbered 1 through N.
Each edge of the graph has a positive integer weight.
This graph is described by three
Nancy is currently standing in the vertex 1. She can reach other vertices by moving along the edges. The cost of using an edge is equal to its weight. Nancy's goal is to reach the vertex N and to minimize the total cost of doing so.
Nancy has a special power she can use to make her travels cheaper.
Whenever she traverses an edge, she can use that special power to make the weight of that edge temporarily negative.
You are given an
Compute and return the minimal total cost of Nancy's journey.
Constraints
- N will be between 1 and 50, inclusive.
- E will be between 1 and 2500, inclusive.
- from, to, weight will each contain exactly E elements.
- from and to will only contain numbers between 1 and N, inclusive.
- There will be a path from node 1 to node N.
- weight will contain numbers between 0 and 100,000, inclusive.
- charges will be between 0 and 1,000,000,000, inclusive.
3
{1,1,2,2,3,3}
{2,3,1,3,1,2}
{1,5,1,10,1,1}
1
Returns: -9
The optimal path for Nancy is 1->2->3, and using her single charge on the last edge.
1
{1}
{1}
{100}
100000
Returns: -10000000
The graph may contain self-loops. Here, the optimal solution is that Nancy uses the self-loop 100,000 times, each time using her special power to change its cost from 100 to -100.
2
{1,1,2}
{2,2,1}
{6,1,4}
2
Returns: -9
There can be multiple edges between vertices.
2
{1}
{2}
{98765}
1000000000
Returns: -98765
Nancy may not be able to use all her charges.
4
{1,2,3,4}
{2,3,4,1}
{1,10,1,1}
8593423
Returns: -60153962
Submissions are judged against all 84 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class NegativeGraphDiv1 with a public method long long findMin(int N, vector<int> from, vector<int> to, vector<int> weight, int charges) · 84 test cases · 2 s / 256 MB per case