Harbin Tech OJ 1348 Shortest path (Floyd algorithm)

Source: Internet
Author: User

Shortest path
Time limit:1000 MS Memory limit:32767 K
Total submit:208(users) Total accepted:31(users) Rating: Special Judge: No
Description
give a weighted graph G for this graph, for the following two operations:
(1) Mark a point of the graph
(2) Find the shortest path length between two points only through the marked points


Input:
The input includes multiple sets of tests, the first line of each group of tests consists of three integers n,m,q,n indicates the number of nodes in the graph, n<=300,
M indicates the number of edges,m<=100000; Q indicates how many operations are performed, q<=100000, all points are numbered 0,1,2,..., N-1,
all of the initial points are unmarked, and the next m line consists of three integer x,y,c, representing an edge length of c,c>0 from X to Y ,
followed by the Q row, each row represents an operation, 0 x indicates the point x is marked, and 1 x y represents the shortest path length for finding x to Y only through the marked point,
n=m=q=0 is the input end.


Output:


The output starts with a line of "Case #:", #表示为第几组测试, starting from 1
for a 0 x operation, if x is already marked, the output "error! At point X ".
for a 1 x y operation, if x or Y is not marked, the output "error! At path x to Y "If you cannot pass the marked section from X
The point reaches Y, output "No such path", otherwise the output requires the shortest path length, and after each set of tests there is a blank line.
Input
The input includes multiple sets of tests, the first line of each group of tests consists of three integers n,m,q,n indicates the number of nodes in the graph, n<=300,
M indicates the number of edges,m<=100000; Q indicates how many operations are performed, q<=100000, all points are numbered 0,1,2,..., N-1,
All of the initial points are unmarked, and the next m line consists of three integer x,y,c, representing an edge length of c,c>0 from X to Y,
followed by the Q row, each row represents an operation, 0 x indicates the point x is marked, and 1 x y represents the shortest path length for finding x to Y only through the marked point,
N=m=q=0 is the input end.

Output
The output starts with a line of "Case #:", #表示为第几组测试, starting from 1
For a 0 x operation, if x is already marked, the output "error! At point X ".
For a 1 x y operation, if x or Y is not marked, the output "error! At path x to Y "If you cannot pass the marked section from X
The point reaches Y, output "No such path", otherwise the output requires a shortest path length, and there is a blank line between each of the two sets of tests.

Sample Input
5 Ten
1 2 6335
0 4 5725
3 3 6963
4 0 8146
1 2 9962
1 0 1943
2 1 2392
4 2 154
2 2 7422
1 3 9896
0 1
0 3
0 2
0 4
0 4
0 1
1 3 3
1 1 1
0 3
0 4
0 0 0
Sample Output

Case 1:
error! At point 4
error! At point 1
0
0
error! At point 3
error! At point 4

To understand the principle of the Floyd algorithm, all the edges of each vertex should be relaxed once, then this is accessible on all sides. For this topic, you can mark one point at a time, all with this point as the center of relaxation all sides

#include <iostream> #include <string.h>using namespace std;const int inf=304;int g[inf][inf];int Vis[inf];    int main () {int n,m,q;    int u,v,w,cse=1;        while (cin>>n>>m>>q,n+m+q) {memset (g,0x1f,sizeof (G));        memset (vis,0,sizeof (VIS));        for (int i=0; i<=n; i++) g[i][i]=0;            for (int i=0; i<m; i++) {cin>>u>>v>>w;        g[u][v]=g[u][v]>w?w:g[u][v];        } cout<< "Case" <<cse++<< ": \ n";        int x, y, Z;            for (int i=0; i<q; i++) {cin>>x;                if (x) {cin>>y>>z; if (!vis[y]| |! Vis[z]) {cout<< "error!                At path ' <<y<< to ' <<z<<endl; } else if (g[y][z]>=0x1f1f1f1f) {cout<< "No such path" <<end                L } ELSE cout<<g[y][z]<<endl;                } else {cin>>y; if (Vis[y]) {cout<< "error!                At point "<<y<<endl;                    } else {vis[y]=1;                            for (int i=0, i<n; i++) {for (int j=0; j<n; J + +) { if (G[i][y]+g[y][j]<g[i][j]) {g[i][                            J]=G[I][Y]+G[Y][J];    }}}}} cout<<endl; } return 0;}


Harbin Tech OJ 1348 Shortest path (Floyd algorithm)

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.