POJ1797 - Heavy Transportation - 条件最短路+思维/二分
2017-04-19 23:05
281 查看
1.题目描述:
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 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
Sample Output
Source
TUD Programming Contest 2004, Darmstadt, Germany
2.题意概述:
给出一个无向图,n个顶点和m条边,每个边都有最大承载量,现在我要从1点运送货物到n点,求能运送货物的最大重量。
3.解题思路:
首先对于能从1到n的某条路线中,能承载货物的最大重量应该是它所有路径中的最小权值。
那么一种做法就是去二分找这个权值(也是我最开始的想法),二分如果当前权值约束下,能保证到达n点,则记录,因为要找最大值,所以再令l = m + 1继续找。成功 AC
之后想想其实这题既然就是要找最大允许权值而不是最短路,那么其实可以利用spfa的贪心思想,把原来的dis记录总路径改成记录到当前顶点下的最大长度就好,这样需要用一个优先队列来维护(想想为什么?)
因为spfa算法每次更新的边就是基于当前解基础上的最优解。
这样优化一下时间快了一个log~
当然看了一下网上其他题解,利用这个贪心思想,还能用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;
}
Heavy Transportation
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 34783 | Accepted: 9177 |
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
Status | Accepted |
---|---|
Time | 610ms |
Memory | 1808kB |
Length | 2049 |
Lang | G++ |
Submitted | 2017-04-07 21:29:14 |
Shared | |
RemoteRunId | 16847813 |
因为spfa算法每次更新的边就是基于当前解基础上的最优解。
这样优化一下时间快了一个log~
Status | Accepted |
---|---|
Time | 282ms |
Memory | 1816kB |
Length | 1866 |
Lang | G++ |
Submitted | 2017-04-07 21:38:07 |
Shared | |
RemoteRunId | 16847873 |
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;
}
相关文章推荐
- hdu 5876 Sparse Graph【最短路+思维】好题
- poj 2112 (二分+二分图多重匹配+最短路)
- HDU2675 - Equation Again - 二分+思维
- [CSU 1817 Bones’s Battery Submit]Floyd+二分+思维
- UVA 10816 Travel in Desert 最短路+二分
- Codeforces 702C Cellular Network【二分+思维】CF的题好多二分啊~
- poj 1724 有限制条件的最短路
- Gym - 101490E Charles in Charge【最短路+二分答案】
- HDU5102 Select 二分 思维
- 【April Fools Day Contest 2016E】【脑洞 define分行用法】Out of Controls 不用条件循环实现最长最短路
- 例题:一对幼兔,一个月后长成小兔,小兔一个月后长成成兔并生下一对幼兔,问n个月后有多少对兔子。这个题主要是学习计算机思维,我们只需要把条件告诉计算机,让计算机算结果
- Codeforces Round #388(Div. 2)D. Leaving Auction【二分+思维】好题~
- POJ 2112 Optimal Milking 【网络流】【二分】【最短路】
- HDU 5936 Difference(思维+二分)——2016年中国大学生程序设计竞赛(杭州)
- Codeforces 622C Not Equal on a Segment【思维预处理+二分】
- hdu 2962 Trucking (二分+最短路Spfa)
- 【心血】算法清单 & 应用(含搜索,动态规划,排序,数据结构,最短路,二分,字符串,其他)
- UVA 10816 Traverl in Desert(最短路+二分)
- 简单的二分算法分析(循环的条件)
- Gym - 100814G Galway Girl (最短路spfa+二分)