Find the Mincost routeTime
limit:1000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2973 Accepted Submission (s): 1197
Problem description Hangzhou has n scenic spots, there are some two-way connection between the scenic spots, now 8600 want to find a tourist route, this route from a point and finally back to a point, assuming the route is V1,v2,.... VK,V1, then must meet the k>2, that is, apart from the starting point to go through at least 2 other different scenic spots, and can not be repeated through the same scenic area. Now 8600 needs you to help him find a route like this, and the less it costs the better.
The first line of input is 2 integers n and m (n <=, M <= 1000), representing the number of scenic spots and the number of roads.
In the next M-line, each row consists of 3 integers a,b,c. Represents a path between A and B and costs C (c <= 100).
Output for each test instance, the minimum value is spent if such a route can be found. If it is not found, output "It's impossible."
Sample Input
3 31 2 12 3 11 3 13 31 2 11 2 32 3 1
Sample Output
3It ' s impossible.
Author8600
Links: http://acm.hdu.edu.cn/showproblem.php?pid=1599
Practice: Before updating the k-1 point as the midpoint, then dp[i][j] and Mp[i][k] and mp[j][k] There must be no duplicate points. Floyd's application problem.
#include <stdio.h> #include <algorithm>using namespace std; #define INF 100000000int mp[110][110];int dp[110][110];int main () {int n,m,a,b,c;while (scanf ("%d%d", &n,&m)! = EOF) {for (int i=1;i<=n;i++) {for (int. j=1;j<=n;j++) {if (i==j) Mp[i][j]=0;elsemp[i][j]=inf;}} for (int i=0;i<m;i + +) {int u,v,c; scanf ("%d%d%d", &u,&v,&c), if (mp[u][v]>c) mp[u][v]=mp[v][u]=c;} for (int i=1;i<=n;i++ {for (int j=1;j<=m;j++) {dp[i][j]=mp[i][j];}} int minn=inf; for (int k=1;k<=n;k++) {for (int. i=1;i<k;i++) {for (int j=1;j<k;j++) {if (i!=j) {int tem=dp[i][j]+mp[i][k]+mp[j][ K]; if (Tem<minn) Minn=tem; } }} for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {if (i!=j&&i!=k&&j!=k) dp[i][j]=min (dp[i][ J],DP[I][K]+DP[K][J]); } } }
HDU 1599 Find the Mincost route non-graphic minimum ring