zoukankan      html  css  js  c++  java
  • POJ 1797 最大运载量

    Heavy Transportation
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 29430   Accepted: 7864

    Description

    Background
    Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
    Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

    Problem
    You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

    Input

    The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

    Output

    The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

    Sample Input

    1
    3 3
    1 2 3
    1 3 4
    2 3 5
    

    Sample Output

    Scenario #1:
    4
    

    题意:

    不同路线的不同边承重不同,每条路线由这条路线中最小称重的边决定最大运载量,问1到n的最大运载量多少


    开始超时代码:

    三重循环的Floyd算法超时

    #include<iostream>
    #include<cmath>
    #include<iomanip>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int inf = 99999999;
    
    
    int path[1005][1005];   //两点间的权值
    
    int main()
    {
    	int i,j,k,a,b,c;
        int T;
        int cases = 1;
        cin>>T;
    	while(T--)
    	{
    	    memset(path,inf,sizeof(inf));
            int n,m;
            cin>>n>>m;
    		for(i=1;i<=m;i++){
                cin>>a>>b>>c;
                path[a][b]=c;
    		}
    
    
    		/*Floyd Algorithm*/
    
    		for(k=1;k<=n;k++)    //k点是第3点
    			for(i=1;i<=n;i++)    //主要针对由i到j的松弛,最终任意两点间的权值都会被分别松弛为最大跳的最小(但每个两点的最小不一定相同)
    				for(j=1;j<=n;j++){
                        if(i==j)
                        continue;
                    if(path[i][k]<inf&&path[k][j]<inf)
    //				if(path[i][k]<path[i][j] && path[k][j]<path[i][j])    //当边ik,kj的权值都小于ij时,则走i->k->j路线,否则走i->j路线
    					// 关键 Floyd-Warshall思想 ,开始允许1中转,到1,2(因为1遍历完了已经把最短赋值给的新的,所以默认包括了旧点),最后到1,2,3.。。n
    						if(path[i][k]<path[k][j])               //当走i->k->j路线时,选择max{ik,kj},只有选择最大跳才能保证连通
    							//这条路径的求最大边
    							path[i][j]=max(path[i][j],path[i][k]);
    						else
    							path[i][j]=max(path[i][j],path[k][j]);
    				}
    		cout<<"Scenario #"<<cases++<<":"<<endl;
    		printf("%d
    
    ",path[1][n]);//在这里path[1][2]表示所有1到2点的路径的最长距离中的最短的那一个,算是Floyd算法的一个巧妙改动
    
    
    	}
    	return 0;
    }
    

    AC:改用Dijkstra算法

    #include<iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int Max = 1005;
    int n, m, edge[Max][Max];
    
    dis[Max];//dis数组存的不再是1到i的最短距离,而是1到i的路线中的边的最小权值
    bool vis[Max];
    int min(int a, int b){
    
        return a < b ? a : b;
    }
    int dijkstra(){
    
        int start = 1, cnt = n - 1;
        dis[start] = 9999999;
       for(int j =1;j<=cnt;j--){
            int k, max_dis = 0;
            for(int i = 2; i <= n; i ++){  //第一个循环利用1-2的确定不断用
                if(!vis[i]){
                   if(edge[start][i] != 0 && dis[i] < min(dis[start], edge[start][i]))//这里dis[i]数组存下的是1点到i点的能运载的最大承重(即路线中边的最小权值)
                        dis[i] = min(dis[start], edge[start][i]);//存入最小的承重
                    if(max_dis < dis[i]){ //不断visit中转的点后,需要比较不同中转点路线(或者直达)的max_dis承重,选出不同路线中最大的dis[k]更新max_dis
                        k = i;
                        max_dis = dis[k];
                    }
                 }
            }
    
            if(k == n) break;
            start = k;
            vis[k] = true;
        }
    
        return dis[n];
    }
    
    int main(){
    
        int k, T, u, v, w;
        int cases=1;
        cin>>T;
        while(T--){
    
            memset(edge, 0, sizeof(edge));
    
            memset(dis, 0, sizeof(dis));
    
            memset(vis, 0, sizeof(vis));
    
            scanf("%d%d", &n, &m);
    
            while(m --){
                scanf("%d%d%d", &u, &v, &w);
                if(edge[u][v] < w){
                    edge[u][v] = w;
                    edge[v][u] = w;
                }
            }
    
            cout<<"Scenario #"<<cases++<<":"<<endl;
            cout<<dijkstra()<<endl;
            cout<<endl;
        }
        return 0;
    
    }
    
    还要理解Dj算法
    

  • 相关阅读:
    06.数组模拟栈-简易计算器
    05.单向环形链表应用 -- 约瑟夫环
    02.数组模拟环形队列
    01.稀疏矩阵与二维数组相互转化
    大数字相乘
    13.打包发布
    悦苗园公益活动
    程序猿打招自己的电子图书馆
    【技能】提高网站可用性
    【微信技能】如何通过微信号知道对方微信的二维码
  • 原文地址:https://www.cnblogs.com/mingrigongchang/p/6246250.html
Copyright © 2011-2022 走看看