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 who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed
on which all streets can carry the weight.

Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem

You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo‘s place) to crossing n (the customer‘s
place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing
of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for
the scenario with a blank line.

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

Sample Output

Scenario #1:
4

Source

TUD Programming Contest 2004, Darmstadt, Germany

题意:求点1到n的最小割。

先尝试了下DFS,结果TLE。

#include <stdio.h>
#include <string.h>

#define maxn 1010
#define maxm maxn * maxn
#define inf 0x3f3f3f3f

int head[maxn], n, m, id, ans, cas = 1;
struct Node {
    int v, c, next;
} E[maxm];
bool vis[maxn];

void addEdge(int u, int v, int c) {
    E[id].v = v; E[id].c = c;
    E[id].next = head[u]; head[u] = id++;

    E[id].v = u; E[id].c = c;
    E[id].next = head[v]; head[v] = id++;
}

void getMap() {
    int u, v, c; id = 0;
    scanf("%d%d", &n, &m);
    memset(head, -1, sizeof(int) * (n + 1));
    while(m--) {
        scanf("%d%d%d", &u, &v, &c);
        addEdge(u, v, c);
    }
}

void DFS(int k, int dis) {
    if(k == n) {
        if(dis > ans) ans = dis;
        return;
    }
    for(int i = head[k]; i != -1; i = E[i].next) {
        if(!vis[E[i].v]) {
            int pre = dis;
            vis[E[i].v] = 1;
            if(E[i].c < dis) dis = E[i].c;
            DFS(E[i].v, dis);
            dis = pre; vis[E[i].v] = 0;
        }
    }
}

void solve() {
    ans = 0;
    memset(vis, 0, sizeof(bool) * (n + 1));
    vis[1] = 1; DFS(1, inf);
    printf("Scenario #%d:\n%d\n\n", cas++, ans);
}

int main() {
    // freopen("stdin.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    while(t--) {
        getMap();
        solve();
    }
    return 0;
}

然后尝试了下Dijkstra,过了..dis数组存储当前点到源点的最小割。

#include <stdio.h>
#include <string.h>

#define maxn 1010
#define maxm maxn * maxn
#define inf 0x3f3f3f3f

int head[maxn], n, m, id, ans, cas = 1;
struct Node {
    int v, c, next;
} E[maxm];
int dis[maxn];
bool vis[maxn];

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

int min(int a, int b) {
    return a < b ? a : b;
}

void addEdge(int u, int v, int c) {
    E[id].v = v; E[id].c = c;
    E[id].next = head[u]; head[u] = id++;

    E[id].v = u; E[id].c = c;
    E[id].next = head[v]; head[v] = id++;
}

void getMap() {
    int u, v, c; id = 0;
    scanf("%d%d", &n, &m);
    memset(head, -1, sizeof(int) * (n + 1));
    while(m--) {
        scanf("%d%d%d", &u, &v, &c);
        addEdge(u, v, c);
    }
}

int getNext() {
    int pos = -1, val = 0;
    for(int i = 1; i <= n; ++i)
        if(dis[i] > val && !vis[i]) {
            val = dis[i]; pos = i;
        }
    return pos;
}

void Dijkstra(int start, int end) {
    memset(dis, 0, sizeof(int) * (n + 1));
    dis[start] = inf;
    int i, u = start, v;
    while(u != -1) {
        vis[u] = 1;
        if(u == end) return;
        for(i = head[u]; i != -1; i = E[i].next) {
            if(!vis[v = E[i].v]) dis[v] = max(dis[v], min(E[i].c, dis[u]));
        }
        u = getNext();
    }
}

void solve() {
    memset(vis, 0, sizeof(bool) * (n + 1));
    Dijkstra(1, n);
    printf("Scenario #%d:\n%d\n\n", cas++, dis[n]);
}

int main() {
    // freopen("stdin.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    while(t--) {
        getMap();
        solve();
    }
    return 0;
}

时间: 2024-08-01 00:06:46

POJ1797 Heavy Transportation 【Dijkstra】的相关文章

POJ 1797 Heavy Transportation【Dijkstra最短路变形】

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

HDU2680 Choose the best route 【Dijkstra】

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7061    Accepted Submission(s): 2300 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU1874 畅通工程续 【Dijkstra】

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 26970    Accepted Submission(s): 9719 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走

HDU2544 最短路 【Dijkstra】

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31182    Accepted Submission(s): 13456 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】

题意:从2到1的所有路径中找出最短的路,并且输出最短路径有几条. 策略:先求出最短路径,然后再找出从2到1的最短路径有几条.最短路径用dijkstra算法来求出,什么是dijkstra算法,简单来说,dijkstra算法就是路径长度递增次序产生最短路径的算法: 基本思想是:把集合V分成两组: (1)S:已求出最短路径的顶点的集合 (2)V-S=T:尚未确定最短路径的顶点集合 将T中顶点按最短路径递增的次序加入到S中, 保证:(1)从源点V0到S中各顶点的最短路径长度都不大于 从V0到T中任何顶点

HDOJ 1874 畅通工程续 【dijkstra】

题意:... 策略:最简单的求最短路径. 代码: #include<stdio.h> #include<string.h> #define MAXN 1005 #define INF 0x3f3f3f3f int di[MAXN], vis[MAXN], n, m; int map[MAXN][MAXN]; void dijkstra(int v) { int i, j; memset(vis, 0, sizeof(vis)); di[v] = 0; vis[v] = 1; for

HDU3790 最短路径问题 【Dijkstra】

最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13336    Accepted Submission(s): 4072 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input

HDU2112 HDU Today 【Dijkstra】

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14743    Accepted Submission(s): 3471 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候

ZOJ--1655--Transport Goods【dijkstra】

题意:某国首都正被攻打,需要运送物资到首都,告诉你n个点,编号1~n,n是首都,剩下的点各有wi重量的物资,m条路,每条路有个货物损失比例,现需要求出最多能运送多少货物到首都. 其实转换一下就是一个最短路问题,边的权值是损失比例,找损失比例最小的那条路,则能运送的货物最多. dist数组存放运成功的比例,初始化为0表示运不成. WA了N发,各种double类型都用int定义的,而且它给的样例即使定义成int对结果也没影响... #include<cstring> #include<str