Poj2240arbitrage determines whether a negative ring exists

Source: Internet
Author: User

Loading editor...

/*

Question:

Ask if I can make a profit after a round trip

Analysis:

Whether the directed graph is in a negative ring, because each vertex may have a negative ring,

Directly use flordAlgorithmJust do it.

*/

# Include <iostream>

# Include <string>

# Include <cstring>

Using namespace STD;

# Define x 35

String City [X], S1, S2;

Double map [x] [x];

Int main ()

{

Freopen ("sum. In", "r", stdin );

Freopen ("sum. Out", "W", stdout );

Int m, n, x, y, CNT = 0;

Double DIS;

While (CIN> N, N)

{

Memset (MAP, 0, sizeof (MAP ));

For (INT I = 0; I <n; I ++)

{

Cin> City [I];

Map [I] [I] = 1;

}

Cin> m;

For (INT I = 0; I <m; I ++)

{

Cin> S1> dis> S2;

For (Int J = 0; j <n; j ++) // locate the start point and end point.

{

If (S1 = City [J])

X = J;

If (s2 = City [J])

Y = J;

}

Map [x] [Y] = DIS; // path length of the Directed Graph

}

For (int K = 0; k <n; k ++) // fload Algorithm

For (INT I = 0; I <n; I ++)

For (Int J = 0; j <n; j ++)

If (Map [I] [J] <map [I] [k] * map [k] [J]) // relaxation operation

Map [I] [J] = map [I] [k] * map [k] [J];

Bool flag = true;

For (INT I = 0; I <n; I ++)

If (Map [I] [I]> 1) // if a negative ring exists

Flag = false;

If (! Flag)

Printf ("case % d: Yes \ n", ++ CNT );

Else

Printf ("case % d: NO \ n", ++ CNT );

}

Return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.