最段路题目变式整理(poj2387,poj2253,poj1797)

poj2253
最段路变形:
1、把每一条路径的和改成每一条路径中最大的区段的长度。
2、起终点的变化。
3、d[]含义的转变
注意输出格式pe了一发

//
//  main.cpp
//  POJ2253_青蛙跳跃
//
//  Created by 陈冉飞 on 2019/8/16.
//  Copyright © 2019 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
#include <cstring>
#define cl(a,b) memset(a,b,sizeof(a))
#define INF 1<<29
#define maxn 205
double map[maxn][maxn],d[maxn];
int vis[maxn];
int n,x,y,i,j,kase = 1;
#include <cmath>

struct node {
    double x,y;
}a[maxn<<2];

//此时dijkstra到最后刷新的就不是某条路的距离之和了,是这条路的最长的段的距离
void dijkstra(){
    for (i = 1; i <= n; i++) d[i] = INF;
    d[1] = 0;
    int v,tem;
    for (i = 1; i <= n; i++) {
        tem = INF;v= 1;
        for (j = 1; j <= n; j++)
            if (!vis[j] && d[j] < tem) {v = j;tem = d[j];}
        vis[v] = 1;
        for (j = 1; j <= n; j++)
            d[j] = min(d[j],max(d[v],map[v][j]));
    }
    printf("Scenario #%d\nFrog Distance = %.3lf\n\n",kase++,d[2]);
}

int main(int argc, const char * argv[]) {
    while(~scanf("%d",&n) & n){
        cl(map, 0);cl(vis, 0);cl(d, 0);
        for (i = 1; i <= n; i++) scanf("%lf%lf",&a[i].x,&a[i].y);
        for (i = 1; i <= n; i++)
            for (j = i+1; j <= n; j ++)
                map[i][j] = map[j][i] = sqrt(pow(a[i].x-a[j].x,2)+pow(a[i].y-a[j].y,2));
//        for (i = 1; i <= n; i++){
//            for (j = 1; j <= n; j++)
//                printf("%.3f    ",map[i][j]);
//            cout<<endl;}
        dijkstra();
    }
    return 0;
}

poj1797
这个题有很多路,每条路都有很多个区间段,每个区间段承载力不同,显然这个要用
de了n边bug发现输入的时候没看到每个case要输入一个n一个m,一直只输入了n,哭了然后到现在还在wa,哭了++,后来发现是输出格式不对,最后要多个换行,终于A了

//
//  main.cpp
//  POJ1797_路径上最大承重,选取路径上的最小值
//
//  Created by 陈冉飞 on 2019/8/16.
//  Copyright © 2019 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
#include <cstring>
#include <cstdio>
#define cl(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f
#define maxn 1005
int map[maxn][maxn],d[maxn],vis[maxn];
int T,n,m,i,j,a,b,c,kase = 1;
#include <cmath>

void dijkstra(){
    int v,tem;
    for (i = 2; i <= n; i++) d[i] = map[1][i];
    d[1] = 0;   //初始化
    for (i = 1; i <= n; i++) {
        tem = 0;v = 0;
        for (j = 1; j <= n; j++)
            if (!vis[j] && d[j] > tem) {
                v = j;
                tem = d[j];
            }
        vis[v] = 1;
        for (j = 1; j <= n; j++) d[j] = max(d[j],min(d[v],map[v][j]));
    }
//    if (kase == 1) puts("");
    printf("Scenario #%d:\n%d\n\n",kase++,d[n]);        //!!!
}

int main(int argc, const char * argv[]) {
    for (scanf("%d",&T); T; T--) {
        cl(map, 0);cl(vis, 0);cl(d, 0);
        scanf("%d%d",&n,&m);
        for (i = 1; i <= m; i++) {scanf("%d%d%d",&a,&b,&c);map[a][b] = map[b][a] = c;}
        dijkstra();
    }
    return 0;
}

8.19复习
Til the Cows Come Home
是真的别扭的板题,初始化map的时候做一次错一次,每次做这个板题都得wa,就是因为初始化的问题,,,还是太菜了

//
//  main.cpp
//  最段路_模版重写
//
//  Created by 陈冉飞 on 2019/8/19.
//  Copyright © 2019 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
#define maxn 1010
int map[maxn][maxn],q,n,a,b,c,vis[maxn],d[maxn];
#define INF 0x3f3f3f

void dijkstra(){
    for (int i = 1; i <= n; i++) d[i] = map[1][i];
    int tem,v;
    for (int i = 1; i <= n; i++) {
        tem = INF;
        for (int j = 1; j <= n; j++) {
            if (!vis[j] && d[j] < tem) {
                tem = d[j];
                v = j;
            }
        }
        vis[v] = 1;
        for (int j = 1; j <= n; j++) {
            if (!vis[j] && d[j] > d[v]+map[v][j]) {
                d[j] = d[v] + map[v][j];
            }
        }
    }
    cout<<d[n]<<endl;
}

int main(int argc, const char * argv[]) {
    scanf("%d%d",&q,&n);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (i == j) map[i][j] = 0;
            else if(!map[i][j]) map[i][j] = INF;
    for (int i = 0; i < q; i++) {
        scanf("%d%d%d",&a,&b,&c);
        if (c < map[a][b]) {
            map[a][b] = c; map[b][a] = c;
        }
    }
    dijkstra();
    return 0;
}

poj2253:Frogger
题意是求若干条路的最小值,其中每条路的值是每条路所有区间的最大值。
因此,在初始化d数组是要初始化为INF(但注意d[1] = 0)。
此外,注意v的初始化,v = 1。(防止v保留上一次的值。)

//
//  main.cpp
//  最段路_重写_所有路的最小值,每条路的最大值
//
//  Created by 陈冉飞 on 2019/8/19.
//  Copyright © 2019 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
#define maxn 205
double map[maxn][maxn],x[maxn<<2],y[maxn<<2],vis[maxn],d[maxn];
int n,kase = 1;
#include <cmath>
#include <cstring>
#define cl(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f

void dijkstra(){
    for (int i = 1; i <= n; i++) d[i] = INF;
    d[1] = 0;
    int tem,v;
    for (int i = 1; i <= n; i++) {
        tem = INF;v = 1;
        for (int j = 1; j <= n; j++) {
            if (!vis[j] && tem > d[j]) {
                tem = d[j];
                v = j;
            }
        }
        vis[v] = 1;
        for (int j = 1; j <= n; j++) {
            d[j] = min(d[j], max(d[v],map[v][j]));
        }
    }
    printf("Scenario #%d\nFrog Distance = %.3lf\n\n",kase++,d[2]);
}

int main(int argc, const char * argv[]) {
    while (scanf("%d",&n) && n) {
        cl(vis, 0);cl(map, 0);cl(d, 0);
        for (int i = 1; i <= n; i++) scanf("%lf%lf",&x[i],&y[i]);
        for (int i = 1; i <= n; i++)
            for (int j = i+1; j <= n; j++)
                map[i][j] = map[j][i] = sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2));
        dijkstra();
    }
    return 0;
}

poj1797
选所有路中的最大值,每条路中的值是每条路中所有区间的最小值

//
//  main.cpp
//  最段路_重写_所有路的最大值,每条路上的最小值
//
//  Created by 陈冉飞 on 2019/8/19.
//  Copyright © 2019 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
#define maxn 1010
int T,n,m,map[maxn][maxn],a,b,c,vis[maxn],d[maxn],kase = 1;
#include <cstring>
#define cl(a,b) memset(a,b,sizeof(a))

void dijkstra(){
    for (int i = 1; i <= n; i++) d[i] = map[1][i];
    d[1] = 0;
    int tem,v;
    for (int i = 1; i <= n; i++) {
        tem = 0; v = 0;
        for (int j = 1; j <= n; j++) {
            if (!vis[j] && tem < d[j]) {
                tem = d[j];
                v = j;
            }
        }
        vis[v] = 1;
        for (int j = 1; j <= n; j++) d[j] = max(d[j],min(d[v],map[v][j]));
    }
    printf("Scenario #%d:\n%d\n\n",kase++,d[n]);
}

int main(int argc, const char * argv[]) {
    for (scanf("%d",&T); T; T--) {
        cl(map, 0);cl(vis, 0);cl(d, 0);
        scanf("%d%d",&n,&m);
        for (int i = 0; i < m; i++) {
            scanf("%d%d%d",&a,&b,&c);
            map[a][b] = map[b][a] = c;
        }
        dijkstra();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值