• hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】


    find the longest of the shortest

    Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2524    Accepted Submission(s): 888


    Problem Description
    Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she doesn't live in the same city, she started preparing for the long journey.We know for every road how many minutes it takes to come from one city to another.
    Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road. It is possible to come from Marica's city to Mirko's no matter which road is closed.
    Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.
     
    Input
    Each case there are two numbers in the first row, N and M, separated by a single space, the number of towns,and the number of roads between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith numbers from 1 to N, Mirko is located in city 1, and Marica in city N.
    In the next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤ V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B, and that it is crossable in V minutes.
     
    Output
    In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
     
    Sample Input
    5 6
    1 2 4
    1 3 3
    2 3 1
    2 4 4
    2 5 7
    4 5 1
    6 7
    1 2 1
    2 3 4
    3 4 4
    4 6 4
    1 5 5
    2 5 2
    5 6
    5 5 7
    1 2 8
    1 4 10
    2 3 9
    2 4 10
    2 5 1
    3 4 7
    3 5 10
     
    Sample Output
    11
    13
    27
     
    题意:最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条,
    题解:我们只需要找出所有路都通畅时的那条最短路,并且依次删去这条路径上的每条边即可,因为如果我们删除的不是这条最短路上的边,我们每次找最短路时依旧会找到这一条
     
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define MAX 1010
    #define INF 0x3f3f3f
    using namespace std;
    int low[MAX];
    int map[MAX][MAX];
    int vis[MAX],p[MAX];
    int n,m;
    void init()
    {
    	int i,j;
    	for(i=0;i<MAX;i++)
    	    for(j=0;j<MAX;j++)
    	        map[i][j]=i==j?0:INF;
    }
    int dj(int v)
    {
    	int i,j,min,next;
    	memset(vis,0,sizeof(vis));
    	memset(low,INF,sizeof(low));
    	low[1]=0;
    	for(i=1;i<=n;i++)
    	{
    		min=INF;
    		for(j=1;j<=n;j++)
    		{
    			if(!vis[j]&&min>low[j])
    			{
    				next=j;
    				min=low[j];
    			}
    		}
    		vis[next]=1;
    		for(j=1;j<=n;j++)
    		{
    			if(!vis[j]&&low[j]>low[next]+map[next][j])
    			{
    				low[j]=low[next]+map[next][j];
    				if(v)
    				p[j]=next;//记录寻找最短路时i的上一个点的位置 
    			}
    		}
    	} 
    	return low[n];
    }
    int main()
    {
    	int a,b,c,i,j;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		init();
    		while(m--)
    		{
    			scanf("%d%d%d",&a,&b,&c);
    			if(map[a][b]>c)
    			map[a][b]=map[b][a]=c;
    		}
    		memset(p,0,sizeof(p));
    		int maxx=dj(1);
    		for(i=n;i!=1;i=p[i])//倒着遍历最短路 
    		{
    			int x=map[i][p[i]];
    			map[i][p[i]]=map[p[i]][i]=INF;
    			maxx=max(maxx,dj(0));
    			map[i][p[i]]=map[p[i]][i]=x;
    		}
    		printf("%d
    ",maxx);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    How to use epoll? A complete example in C
    分享:libzip 0.11 发布,C 语言的 zip 压缩开发包
    linux AIO (异步IO) 那点事儿
    通过引用计数解决野指针的问题(C&C++)
    [原]浅谈几种服务器端模型——反应堆模式(epoll 简介) _Boz 博客园
    基于EPOLL写的HTTP服务器(加入了线程池)_没落都城_新浪博客
    jQuery检测浏览器名称和版本信息
    Jquery.ajax中dataType不可少
    jquery 手风琴效果
    ie下ajax错误:由于出现错误 c00ce56e 而导致此项操作无法完成
  • 原文地址:https://www.cnblogs.com/tonghao/p/5003065.html
Copyright © 2020-2023  润新知