Heavy Transportation(POJ - 1797)
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 57578 | Accepted: 14456 |
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
题意:求所有能从 1 ---> N 节点的所有路径,假如共m条路。 每一条路径中最小的那一段的最大值。
题解:
二分法:二分答案,再找1 到 N 的路径。当前二分值小的路径都不选用。若1到N存在路径。再把答案调大。若不能,调小答案。
AC代码:
//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mset(a, n) memset(a, n, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<int,PII> PPI;
typedef pair<ll, ll> PLL;
const int N = 1000+3;
int V,E;
struct node {
int v;
int d;
node(int v1,int d1): v(v1),d(d1){}
};
pair<int,int> P[N];
vector<node> G[N];
bool vis[N];
int dis[N];
int ans = 0;
bool spfa(const int lim)
{
memset(vis, false, sizeof(vis));
mset(dis,0x3f3f);
queue<int> Q;
Q.push(1);
dis[1] = 0;
vis[1] = true;
while(!Q.empty()){
int u = Q.front();Q.pop();
vis[u] = false;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].v;
int val = G[u][i].d;
if(val<lim) continue;
if(dis[v] > (dis[u]+val)){
dis[v] = dis[u]+val;
if(!vis[v]) {
Q.push(v);
vis[v] = true;
}
}
}
}
if(dis[V] < 1061109567) return true;
else return false;
}
int main()
{
IO;
int tot;
cin>>tot;
for(int test = 1;test<=tot;test++){
cin>>V>>E;
for(int i=1;i<=V;i++) G[i].clear();
for(int i=0;i<E;i++){
int u,v,val;
cin>>u>>v>>val;
G[u].pb(node(v,val));
G[v].pb(node(u,val));
}
int l = 0, r = 1000001, mid;
while((r-l) >= 0){
mid = (r-l)/2+l;
if((spfa(mid))){
ans = mid;
l = mid+1;
}
else{
r = mid-1;
}
}
printf("Scenario #%d:\n",test);
printf("%d\n\n",ans);
}
return 0;
}
/*
10
3 3
1 3 4
2 3 1
1 2 2
4 6
1 2 1
1 3 3
1 4 4
2 3 1
3 4 1
2 4 5
5 7
1 2 2
2 5 2
1 3 4
1 4 7
3 4 1
2 3 1
3 5 6
*/
Dijkstra算法改编: dis[v] = max( min(dis[u], map[u][v]), dis[v] ) , 其中 dis[u/v] 表示1 到 u/v 的路径中最小的那一段。
AC代码:
// 以后再补。
嘻嘻
最小生成树改编: 其实你可以把这道题看成是一道最大生成树。然后找到该树中最小的边。就是答案啦!
AC代码:
以后再补。。。
小博认为以上三种方法。第二种最好!有错的地方还请大家不吝赐教啊