Hdu 1385 Minimum Transport Cost (floyd algorithm)

Source: Internet
Author: User

Looks like ····················

This algorithm is quite unfamiliar! Continue learning !!!!

++

======================================

++

-------------------------------------------------

======================================

# Include <stdio. h>

# Include <string. h>
# Deprecision MAX 10000000
# Define N 1001


Int a [N] [N], B [N], next [N] [N];
Int n, len;


Void floyd ()
{
Int I, j, k, numeric;
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
Next [I] [j] = j;
For (k = 1; k <= n; k ++)
{
For (I = 1; I <= n; I ++)
{
If (I = k | a [I] [k] =-1)
Continue;
For (j = 1; j <= n; j ++)
{
If (a [k] [j] =-1 | I = k | j = k)
Continue;
Signature = a [I] [k] + a [k] [j] + B [k];
If (a [I] [j] =-1 | a [I] [j]> else)
{
A [I] [j] = bytes;
Next [I] [j] = next [I] [k];
}
Else if (a [I] [j] = alias)
{
If (next [I] [j]> next [I] [k])
Next [I] [j] = next [I] [k];
}
}
}
}
}
Void path (int start, int end)
{
If (end = next [start] [end])
Printf ("% d --> % d \ n", start, end );
Else
{
Printf ("% d -->", start );
Path (next [start] [end], end );
}
}


Int main ()
{
Int I, j, start, end;
While (scanf ("% d", & n), n)
{
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
A [I] [j] = MAX;
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
Scanf ("% d", & a [I] [j]);
For (I = 1; I <= n; I ++)
Scanf ("% d", & B [I]);
Floyd ();
While (scanf ("% d", & start, & end )! = EOF)
{

If (start =-1 | end =-1)
Break;
Printf ("From % d to % d: \ n", start, end );
If (start = end) // note: the output format when the start point and the end point are the same!
Printf ("Path: % d \ n", start );
Else
{
Printf ("Path :");
Path (start, end );
}
Printf ("Total cost: % d \ n", a [start] [end]);
}
}
Return 0;

}

 

Related Article

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.