poj1797 Heavy Transportation(最短路变形)

题目大意:有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量

解题思路:其实这个求最大边可以近似于求最短路,只要修改下找最短路更新的条件就可以了

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn=1010;
const int INF=0x3f3f3f3f;

int lowc[maxn],cost[maxn][maxn];
bool vis[maxn];

int T;
int n,m;

void Dijkstra()
{
    for(int i=2;i<=n;i++)
    {
        lowc[i]=cost[1][i];
    }
    lowc[1]=0;
    vis[1]=1;
    for(int i=1;i<=n;i++)
    {
        int k=-1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&(k==-1||lowc[j]>lowc[k]))//此处是找出最大的值,因为这里wa了好几发
            {
                k=j;
            }
        }
        if(k==-1) break;
        vis[k]=1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&lowc[j]<min(lowc[k],cost[j][k])) //lowc[j]=max(lowc[j],max(lowc[k],cost[k][j]));
            {
                lowc[j]=min(lowc[k],cost[j][k]);
            }
        }
    }
}

void init()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j) cost[i][j]=0;
            else cost[i][j]=0;
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>T;
    int kase=1;
    while(T--)
    {
        cin>>n>>m;
        memset(vis,0,sizeof(vis));
        init();
        for(int i=1;i<=m;i++)
        {
            int u,v,w;
            cin>>u>>v>>w;
            cost[u][v]=cost[v][u]=w;
        }
        Dijkstra();
        printf("Scenario #%d:\n",kase++);
        printf("%d\n\n",lowc[n]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Fy1999/p/9463980.html

时间: 2024-10-29 01:13:03

poj1797 Heavy Transportation(最短路变形)的相关文章

POJ 1797 Heavy Transportation (最短路变形)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 20364   Accepted: 5401 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K          Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there reall

POJ 1797 Heavy Transportation (Dijkstra变形)

F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand busines

POJ-1797 Heavy Transportation(最大生成树)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 39963   Accepted: 10507 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

POJ1797 Heavy Transportation 【Dijkstra】

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 21037   Accepted: 5569 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

poj1797 Heavy Transportation (Kruskal 算法)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 22796   Accepted: 6053 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

poj1797 - Heavy Transportation(最大边,最短路变形spfa)

题目大意: 给你以T, 代表T组测试数据,一个n代表有n个点, 一个m代表有m条边, 每条边有三个参数,a,b,c表示从a到b的这条路上最大的承受重量是c, 让你找出一条线路,要求这条线路上的最大的承重, 在所有其他线路最小. 题目分析: 这里只要将spfa进行一下变形就可以解决这问题了. 首先 我们的dist数组,起点位置要初始化为 INF, 其他位置初始化为 0 然后我们更新 dist 数组, 结果输出 dist[n]就行了 为什么这样写: 因为我们每次要找 所有路径中的最大边的最小一个,

POJ1797 Heavy Transportation (堆优化的Dijkstra变形)

Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place

poj1797——Heavy Transportation(最大生成树)

Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane t