zoukankan      html  css  js  c++  java
  • K

    来源poj2263

    Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions that apply for the roads along the path you want to drive.

    Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities.

    Input

    The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network.
    Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions.
    The last line of the test case contains two city names: start and destination.
    Input will be terminated by two values of 0 for n and r.

    Output

    For each test case, print three lines:
    a line saying "Scenario #x" where x is the number of the test case
    a line saying "y tons" where y is the maximum possible load
    a blank line

    Sample Input

    4 3
    Karlsruhe Stuttgart 100
    Stuttgart Ulm 80
    Ulm Muenchen 120
    Karlsruhe Muenchen
    5 5
    Karlsruhe Stuttgart 100
    Stuttgart Ulm 80
    Ulm Muenchen 120
    Karlsruhe Hamburg 220
    Hamburg Muenchen 170
    Muenchen Karlsruhe
    0 0

    Sample Output

    Scenario #1
    80 tons

    Scenario #2
    170 tons

    找到n的最大的载重,似乎之前做过,只要用dijkstar变形一下,每次找最大的来搜,收到end就是最大的了

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include <iomanip>
    #include<cmath>
    #include<float.h> 
    #include<string.h>
    #include<algorithm>
    #define sf scanf
    #define pf printf
    #define scf(x) scanf("%d",&x)
    #define scff(x,y) scanf("%d%d",&x,&y)
    #define prf(x) printf("%d
    ",x) 
    #define mm(x,b) memset((x),(b),sizeof(x))
    #include<vector>
    #include<queue>
    //#include<map>
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=a;i>=n;i--)
    typedef long long ll;
    const ll mod=1e9+7;
    const double eps=1e-8;
    const int inf=0x3f3f3f3f;
    using namespace std;
    const double pi=acos(-1.0);
    const int N=1e5+10;
    char name[205][35];
    int map[205][205];
    int visit[205];
    int value[205];
    int find(int n,char b[35])
    {
    	rep(i,0,n)
    	{
    		if(strcmp(b,name[i])==0)
    		return i;
    	}
    	return -1;
    }
    int dijkstra(int n,int star,int end)
    {
    	int pos,MAX=0;
    	value[star]=inf;
    	rep(i,0,n)
    	{
    		if(map[star][i]!=-1)
    		value[i]=map[star][i];
    		if(MAX<value[i]&&i!=star)
    		{
    			MAX=value[i];
    			pos=i;
    		}
    	}
    	visit[star]=1;
    	visit[pos]=1;
    	if(pos==end) return MAX;
    	rep(i,1,n)
    	{
    		MAX=0;
    		rep(j,0,n)
    		{
    			if(map[pos][j]!=-1&&visit[j]==0)
    			{
    				if(value[j]==-1) value[j]=min(map[pos][j],value[pos]);
    				else value[j]=max(value[j],min(map[pos][j],value[pos]));
    			}
    		}
    		rep(j,0,n)
    		{
    			if(MAX<value[j]&&j!=star&&visit[j]==0)
    			{
    				pos=j;
    				MAX=value[j];
    			}
    		}
    		visit[pos]=1;
    		if(pos==end) return value[end];
    	}
    	return value[end];
    }
    int main()
    {
    	int n,m,k;
    	int bits=1;
    	char a[35],b[35];
    	while(~scff(n,m)&&n)
    	{
    		mm(visit,0);
    		mm(value,-1);
    		mm(map,-1);
    		int cas=0;
    		while(m--)
    		{
    			sf("%s%s%d",a,b,&k);
    			int x,y;
    			x=find(cas,a);
    			if(x==-1)
    			{
    				x=cas;
    				strcpy(name[cas++],a);
    			}
    			y=find(cas,b);
    			if(y==-1)
    			{
    				y=cas;
    				strcpy(name[cas++],b);
    			}
    			map[x][y]=map[y][x]=k;
    		}
    		sf("%s%s",a,b);
    		int star,end;
    		star=find(cas,a);end=find(cas,b);
    		int ans=dijkstra(n,star,end);
    		pf("Scenario #%d
    %d tons
    
    ",bits++,ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    App架构经验总结
    通过C语言程序改动控制台的背景和前景颜色
    java实现读取yaml文件,并获取值
    java 实现yaml 数据转json与map
    yaml标记语言的简介
    重新学习之spring第二个程序,配置AOP面向切面编程
    重新学习之spring第一个程序,配置IOC容器
    重新学习Spring之核心IOC容器的底层原理
    hibernate之xml映射文件关系维护,懒加载,级联
    hibernate映射xml文件配置之一对多,多对多
  • 原文地址:https://www.cnblogs.com/wzl19981116/p/9503217.html
Copyright © 2011-2022 走看看