您的位置:首页 > 其它

POJ1797 - Heavy Transportation - 条件最短路+思维/二分

2017-04-19 23:05 281 查看
1.题目描述:

Heavy Transportation

Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 34783 Accepted: 9177
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
2.题意概述:
给出一个无向图,n个顶点和m条边,每个边都有最大承载量,现在我要从1点运送货物到n点,求能运送货物的最大重量。

3.解题思路:

首先对于能从1到n的某条路线中,能承载货物的最大重量应该是它所有路径中的最小权值。

那么一种做法就是去二分找这个权值(也是我最开始的想法),二分如果当前权值约束下,能保证到达n点,则记录,因为要找最大值,所以再令l = m + 1继续找。成功 AC

StatusAccepted
Time610ms
Memory1808kB
Length2049
LangG++
Submitted2017-04-07 21:29:14
Shared
RemoteRunId16847813
之后想想其实这题既然就是要找最大允许权值而不是最短路,那么其实可以利用spfa的贪心思想,把原来的dis记录总路径改成记录到当前顶点下的最大长度就好,这样需要用一个优先队列来维护(想想为什么?)
因为spfa算法每次更新的边就是基于当前解基础上的最优解。

这样优化一下时间快了一个log~

StatusAccepted
Time282ms
Memory1816kB
Length1866
LangG++
Submitted2017-04-07 21:38:07
Shared
RemoteRunId16847873
当然看了一下网上其他题解,利用这个贪心思想,还能用prim来解,这种做法以后我再补
4.AC代码:

二分:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
struct node
{
int v, w;
node(int a, int b) { v = a; w = b; }
};
vector<node> mp
;
//vector<int> edge;
int dis
;
bool vis
;
void spfa(int sta, int n, int min_len)
{
memset(vis, 0, sizeof(vis));
fill(dis, dis + n + 1, INF);
deque<int> q;
vis[sta] = 1;
dis[sta] = 0;
q.push_back(sta);
while (!q.empty())
{
int u = q.front();
q.pop_front();
vis[u] = 0;
int sz = mp[u].size();
for (int i = 0; i < sz; i++)
{
int v = mp[u][i].v;
int w = mp[u][i].w;
if (dis[v] > dis[u] + w && w >= min_len)
{
dis[v] = dis[u] + w;
if (!vis[v])
{
vis[v] = 1;
if (!q.empty() && dis[v] >= dis[q.front()])
q.push_front(v);
else
q.push_back(v);
}
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int t, n, m;
scanf("%d", &t);
for (int i = 1; i <= t; i++)
{
if (i > 1)
puts("");
printf("Scenario #%d:\n", i);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
mp[i].clear();
int max_weight = 0;
while (m--)
{
int u, v, weight;
scanf("%d%d%d", &u, &v, &weight);
mp[u].push_back(node(v, weight));
mp[v].push_back(node(u, weight));
max_weight = max(max_weight, weight);
}
int l = 0, r = max_weight, mid, ans = -1;
while (l <= r)
{
mid = l + (r - l) / 2;
spfa(1, n, mid);
if (dis
!= INF)
{
ans = mid;
l = mid + 1;
}
else
r = mid - 1;
}
printf("%d\n", ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}


对边进行spfa:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
struct node
{
int v, w;
node(int a, int b) { v = a; w = b; }
friend bool operator< (node a, node b)
{
if (a.w == b.w)
return a.v < b.v;
return a.w < b.w;
}
};
vector<node> mp
;
int dis
;
bool vis
;
void spfa(int sta, int n)
{
memset(vis, 0, sizeof(vis));
memset(dis, 0, sizeof(dis));
deque<int> q;
vis[sta] = 1;
dis[sta] = INF;
q.push_back(sta);
while (!q.empty())
{
int u = q.front();
q.pop_front();
vis[u] = 0;
int sz = mp[u].size();
for (int i = 0; i < sz; i++)
{
int v = mp[u][i].v;
int w = mp[u][i].w;
int c = min(w, dis[u]);
if (c > dis[v])
{
dis[v] = c;
if (!vis[v])
{
vis[v] = 1;
if (!q.empty() && dis[v] >= dis[q.front()])
q.push_front(v);
else
q.push_back(v);
}
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int t, n, m;
scanf("%d", &t);
for (int i = 1; i <= t; i++)
{
if (i > 1)
puts("");
printf("Scenario #%d:\n", i);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
mp[i].clear();
while (m--)
{
int u, v, weight;
scanf("%d%d%d", &u, &v, &weight);
mp[u].push_back(node(v, weight));
mp[v].push_back(node(u, weight));
}
spfa(1, n);
printf("%d\n", dis
);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm 算法 poj