hdu1595(spfa) find the longest of the shortest

来源:互联网 发布:义乌美工培训学校排名 编辑:程序博客网 时间:2024/04/19 20:59

题目链接:

huangjing

思路:因为这个女人是从最短路走,那么首先找出最短路,然后拆边,然后再找最短路,最后求出最长时间即可。

代码里还有递归法求最短路径。。

题目:

find the longest of the shortest

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


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 61 2 41 3 32 3 12 4 42 5 74 5 16 71 2 12 3 43 4 44 6 41 5 52 5 25 6 55 71 2 81 4 102 3 92 4 102 5 13 4 73 5 10
 

Sample Output
111327
 

Author
ailyanlu
 

Source
HDU 2007-Spring Programming Contest - Warm Up (1)
 

Recommend
8600   |   We have carefully selected several similar problems for you:  1599 1596 1598 1142 1217 
代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<vector>#include<cmath>#include<string>#include<queue>#define eps 1e-9#define ll long long#define INF 0x3f3f3f3fusing namespace std;const int maxn=1000+10;bool vis[maxn],used[maxn][maxn];int dis[maxn],n,m,pre[maxn],cnt,head[maxn];queue<int>Q;struct Edge{    int to,next,val;}edge[maxn*maxn];void add_edge(int x,int y,int z){    edge[++cnt].to=y;    edge[cnt].val=z;    edge[cnt].next=head[x];    head[x]=cnt;}int spfa(int ok){    while(!Q.empty()) Q.pop();    memset(vis,false,sizeof(vis));    memset(dis,INF,sizeof(dis));    vis[1]=true;    dis[1]=0;    Q.push(1);    if(ok)       pre[1]=-1;    while(!Q.empty())    {        int top=Q.front();        Q.pop();        for(int i=head[top];i!=-1;i=edge[i].next)        {            int xx=edge[i].to;            int yy=edge[i].val;            if(used[top][xx]&&dis[xx]>dis[top]+yy)            {                dis[xx]=dis[top]+yy;                if(ok)                    pre[xx]=top;                if(!vis[xx])                {                    Q.push(xx);                    vis[xx]=true;                }            }        }        vis[top]=false;    }    return dis[n];}void print(int x){    if(pre[x]==-1)    {        printf("1");        return;    }    print(pre[x]);    printf("-->%d",x);}int main(){    int ans,u,v,w;    while(~scanf("%d%d",&n,&m))    {        memset(head,-1,sizeof(head));        cnt=0;        for(int i=1;i<=m;i++)        {            scanf("%d%d%d",&u,&v,&w);            add_edge(u,v,w);            add_edge(v,u,w);        }        for(int i=1;i<=n;i++)            for(int j=i+1;j<=n;j++)                   used[i][j]=used[j][i]=true;        ans=spfa(1);       // print(n);        for(int i=n;pre[i]!=-1;i=pre[i])        {             u=i,v=pre[i];             used[u][v]=used[v][u]=false;             ans=max(ans,spfa(0));             used[u][v]=used[v][u]=true;        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击