关闭

hdu 1595 find the longest of the shortest(dijkstra)

151人阅读 评论(0) 收藏 举报
分类:

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

#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;
}


0
0
查看评论

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 revenge.Since she doesn't live in the same cit...
  • libin56842
  • libin56842
  • 2013-12-06 19:43
  • 1832

HDU 1595 find the longest of the shortest(枚举,最短路)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1595 题目: find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) ...
  • shuangde800
  • shuangde800
  • 2012-10-14 01:44
  • 1433

hdu 1595 find the longest of the shortest (Dijkstra)

find the longest of the shortestTime Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3347 Acce...
  • Merry_hj
  • Merry_hj
  • 2017-07-18 18:11
  • 123

HDU 1595 find the longest of the shortest (Dijkstra)

find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi...
  • secretbase_
  • secretbase_
  • 2015-03-08 19:03
  • 218

HDU 1595 find the longest of the shortest(Dijkstra)

HDU 1595 find the longest of the shortest(Dijkstra) http://acm.hdu.edu.cn/showproblem.php?pid=1595 题意:有一个N个点M条边的无向图,现在要从1号点走到N号点去.但是该图中有一条边(不知道是哪条)不能走...
  • u013480600
  • u013480600
  • 2014-07-12 14:50
  • 723

HDU 1595 find the longest of the shortest (Dijkstra算法)

find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi...
  • aozil_yang
  • aozil_yang
  • 2016-06-08 23:58
  • 501

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

find the longest of the shortest 题目链接:Problem - 1595 题目大意:现有一个无向图,其中某一条边不存在(要删除),求所有情况中最短路里的最大值。 题目分析: 思路一:枚举m条边,一一删除进行spfa求出最短路,然后比较出最大值。(超时) 思路二:...
  • GrEedWish_X
  • GrEedWish_X
  • 2015-09-15 16:57
  • 247

hdu 1595 find the longest of the shortest (dijkstra)

/* 假设图中某条路径被堵死,它的最坏情况下的最短路径是多少? 基本算法就是先求出最短路径,然后假设最短路径中的某一条边被堵死, 再求最短路,取这些最短路的最大值即可。 */ # include # include # include using namespace std; # define in...
  • lp_opai
  • lp_opai
  • 2015-08-31 19:44
  • 355

1595 hdu find the longest of the shortest

题目地址: 题解:题目意思为从起点到终点的最短路中有一条路不能通过了,求到从起点到终点的最短距离。 可以先找出从起点到终点的最短距离,并将路径保存下来,然后枚举最短路径中的所有路径,求 出从起点到终点的最短路径中最长的一条。   #include #include #incl...
  • luo964061873
  • luo964061873
  • 2012-09-06 15:40
  • 235

hdu 1595 find the longest of the shortest

点击打开链接hdu 1595 思路:最短路+优先队列+Dijstra+枚举边 分析: 1 题目要求的是删掉一条边之和求出的最短路中的最大值。 2 很明显,肯定是要先求出原图的最短路并且记录父亲节点。现在我们可以想,如果要枚举所有的边,显然这个是不可能的实现的。所以我们仔细分析可以...
  • cgl1079743846
  • cgl1079743846
  • 2012-10-16 11:47
  • 701
    常来看看
    个人资料
    • 访问:134061次
    • 积分:7381
    • 等级:
    • 排名:第3567名
    • 原创:601篇
    • 转载:8篇
    • 译文:0篇
    • 评论:42条