hdu 1595 find the longest of the shortest(dijkstra)

原创 2016年05月30日 20:16:19

我写的超时了,看了别人的解题报告难过

#include <stdio.h>
#include <string.h>
#define INF 99999999
int map[1001][1001];
int vis[1001],dis[1001],link[1001];
int n,m;

int max(int a, int b)
{
    return a > b ? a : b;
}

void dijkstra(int s, int flag)
{
    int i,j,min,pos;
    memset(vis,0,sizeof(vis));
    for(i = 1; i <= n; ++i)
        dis[i] = INF;
    dis[s] = 0;
    for(i = 1; i < n; ++i)
    {
        min = INF;
        for(j = 1; j <= n; ++j)
        {
            if(dis[j] < min && !vis[j])
                min = dis[pos = j];
        }
        vis[pos] = 1;
        for(j = 1; j <= n; ++j)
            if(dis[pos] + map[pos][j] < dis[j])
            {
                dis[j] = dis[pos] + map[pos][j];
                if(flag)//只记录最短那条路的父节点
                    link[j] = pos;
            }
    }
}

int main()
{
    int i,j,x,y,w;
    while(scanf("%d %d",&n,&m) != EOF)
    {
        for(i = 1; i <= n; ++i)
        {
            for(j = 1; j <= n; ++j)
                map[i][j] = INF;
            map[i][i] = 0;
        }
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&w);
            if(w < map[x][y])
                map[x][y] = map[y][x] = w;
        }
        memset(link,0,sizeof(link));
        dijkstra(1,1);
        int ans = dis[n];
        for(i = n; i != 1; i = link[i])
        {
            int tem = map[i][link[i]];//当前最短路到父节点的距离先存起来
            map[i][link[i]] = map[link[i]][i] = INF;
            dijkstra(1,0);
            ans = max(ans,dis[n]);
            map[i][link[i]] = map[link[i]][i] = tem;//恢复当前的最短路
        }
        printf("%d\n",ans);
    }
    return 0;
}


版权声明:如果发现错误或者其他什么不足之处。请留言。

相关文章推荐

HDU1595 find the longest of the shortest(最短路,Dijkstra)

题目: find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 3...

HDU - 1595 find the longest of the shortest(最短路Dijkstra+枚举删边)

题意:某人要从 1 号城市到 n 号城市。有一条最短路径,在这条路径上可能会有一条边在维修,问此时从1~n的最短路径可能的最大值。读起来略拗口。慢慢理解就是了,不难懂。 思路:先用最短路径保存路径,...

hdu1595—find the longest of the shortest(dijkstra+枚举)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1595 find the longest of the shortest Time Limi...

hdu 1595 find the longest of the shortest 最短路dijkstra+枚举

题目链接 题意:给出n个点,m条边,求去除任意一条边的最长最短路。 这题先用求一次最短路,记录下路径。如果去除的边不在路径上,那么最短路不变。所以我们只要考虑去除最短路上的边的情况。枚举最短路上的边删...

hdu1595 find the longest of the shortest (dijkstra+记录路径)

find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/3276...

HDU1595:find the longest of the shortest(Dijkstra)

Problem Description Marica is very angry with Mirko because he found a new girlfriend and she seeks ...

hdu 1595 find the longest of the shortest (dijkstra + spfa)

find the longest of the shortest 题目链接:Problem - 1595 题目大意:现有一个无向图,其中某一条边不存在(要删除),求所有情况中最短路里的最大值。 ...

hdu1595 find the longest of the shortest(dijkstra+枚举)

http://acm.hdu.edu.cn/showproblem.php?pid=1595 题意:玛丽卡很生气因为她知道她男友米尔科交到了新的女朋友,于是要去找他算账。但是他们不在一个城市,玛...

hdu 1595 find the longest of the shortest (Dijkstra+路径处理)

Problem Description Marica is very angry with Mirko because he found a new girlfriend and she seeks...

HDU1595find the longest of the shortest(SPFA,删边求最短路中的最长路)

find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/3276...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1595 find the longest of the shortest(dijkstra)
举报原因:
原因补充:

(最多只允许输入30个字)