最短路小变形 - 最大边权中的最小值

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 . Dijkstra 变形搞一搞, d[i]中所存的不在是最短距离,而是从源点到 i 点最小边权中的最大值
代码有好几个坑点啊,一顿调啊,定义的变量由于粗心,定义了个全局的又定义了个局部的,其实也只是在局部中用到,交上去一直是WA,后来发现问题,改了就A 了,后来又改成局部的变量,结果一直是RE,我靠,好神奇的东西,看来以后定义的变量都要仍在外面。

代码示例 :
const int inf = 1<<29;
int n, m;
struct node
{
    int to, cost;
    node(int _t, int _c):to(_t),cost(_c){}
};
vector<node>edge[1005];
int d[1005];
bool vis[1005];

struct qnode
{
    int v, c;
    qnode(int _v, int _c):v(_v),c(_c){}
    friend bool operator< (qnode n1, qnode n2){  // 注意重载运算符 <
        return n1.c < n2.c;
    }
};

void dij(){
    memset(vis, false, sizeof(vis));
    memset(d, 0, sizeof(d));  
    priority_queue<qnode>que;
    //priority_queue(qnode)que; 
    while(!que.empty()) que.pop();
    
    d[1] = inf;
    que.push(qnode(1, d[1]));
    //vis[1] = true;
    while(!que.empty()){
        qnode tem = que.top();
        que.pop();
        int v = tem.v;
        if (vis[v]) continue;
        vis[v] = true;
        
        for(int i = 0; i < edge[v].size(); i++){
            int to = edge[v][i].to;
            int cost = edge[v][i].cost;
            if (!vis[to] && min(d[v], cost) > d[to]) {
                d[to] = min(d[v], cost);
                que.push(qnode(to, d[to]));
            }
        }
    }
}

int main() {
    int t;
    int a, b, c;
    int k = 1;
    
    cin >>t;
    while(t--){
        for(int i = 1; i <= n; i++){
            edge[i].clear();
        } 
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= m; i++){
            scanf("%d%d%d", &a, &b, &c);
            edge[a].push_back(node(b, c));
            edge[b].push_back(node(a, c));
        }
        dij();
        printf("Scenario #%d:\n", k++);
        printf("%d\n\n", d[n]);
        
    }

    return 0;
}

 2 . SPFA

 

const int inf = 1<<29;
int n, m;
struct node
{
    int to, cost;
    node(int _t, int _c):to(_t), cost(_c){}
};
vector<node>edge[1005];
bool vis[1005];
int d[1005];

void spfa(){
    queue<int>que;
    while(!que.empty()) que.pop();
    memset(vis, false, sizeof(vis));
    for(int i = 1; i <= n; i++) d[i] = 0;
    que.push(1);
    d[1] = inf;
    
    while(!que.empty()){
        int v = que.front();
        que.pop();
        vis[v] = false;
        
        for(int i = 0; i < edge[v].size(); i++){
            int to = edge[v][i].to;
            int cost = edge[v][i].cost;
            if (min(d[v], cost) > d[to]){
                d[to] = min(d[v], cost);
                if (!vis[to]){
                    vis[to] = true;
                    que.push(to);
                }
            }
        } 
    }
}

int main() {
    int t;
    int a, b, c;
    int k = 1;
    
    cin >> t;
    while(t--){
        for(int i = 1; i <= n; i++){
            edge[i].clear();
        }
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= m; i++){
            scanf("%d%d%d", &a, &b, &c);
            edge[a].push_back(node(b, c));
            edge[b].push_back(node(a, c));
        }
        spfa();
        printf("Scenario #%d:\n", k++);
        printf("%d\n\n", d[n]);
    }

    return 0;
}

 3 . 最大生成树(kruskal)

const int inf = 0x3f3f3f3f;
int n, m;
struct node
{
    int from, to, cost;
}edge[eps];

bool cmp(node a, node b){
    return a.cost > b.cost;
}
int f[1005];
int ans;

int find(int x){
    int r = x;
    while(r != f[r]){
        r = f[r];
    }
    return r;
    //if (f[x] == -1) return x;
    //else return f[x] = find(f[x]);
}

void kru(){
    for(int i = 1; i <= n; i++) f[i] = i;    
    ans = inf;
    for(int i = 1; i <= m; i++){
        int from = edge[i].from;
        int to = edge[i].to;
        int cost = edge[i].cost;
        int t1 = find(from);
        int t2 = find(to);
        int f1 = find(1), f2 = find(n); // 结束的条件 1与n连通
        if (f1 == f2) break;
        if (t1 != t2){
            ans = min(ans, cost);
            f[t1] = t2;
        }
        
    }
}

int main() {
    int t;
    int a, b, c;
    int k = 1;
    
    cin >> t;
    while(t--){
        scanf("%d%d", &n, &m);
        for(int i = 1;i <= m; i++){
            scanf("%d%d%d", &a, &b, &c);
            edge[i].from = a;
            edge[i].to = b;
            edge[i].cost = c;            
        }
        sort(edge+1, edge+1+m, cmp);
        kru();
        printf("Scenario #%d:\n", k++);
        printf("%d\n\n", ans);
    }
    return 0;
}

 4 . 最大生成树 (prim)

const int inf = 1<<29;
int n, m;
int edge[1005][1005];
int d[1005];
bool vis[1005];

struct node
{
    int v, c;
    node(int _v, int _c):v(_v), c(_c){}
    friend bool operator< (node n1, node n2){
        return n1.c < n2.c;
    }
};
int ans;
void prim(){
    ans = inf;
     priority_queue<node>que;
    for(int i = 1; i <= n; i++){
        d[i] = edge[1][i];
        if (d[i]) que.push(node(i, d[i]));
    }
    d[1] = inf;
    memset(vis, false, sizeof(vis));
   
    
    while(!que.empty()){
        node tem = que.top();
        que.pop();
        
        int v = tem.v;
        int c = tem.c;
        ans = min(ans, c);
        if (v == n) return;
        if (vis[v]) continue;
        vis[v] = true;
        
        for(int i = 1; i <= n; i++){
            if (!vis[i] && edge[v][i] > d[i]){
                d[i] = edge[v][i];
                que.push(node(i, d[i])); 
            }
        }
    }
}

int main() {
    int t;
    int a, b, c;
    int k = 1;
    
    cin >> t;
    while(t--){
        scanf("%d%d", &n, &m);
        memset(edge, 0, sizeof(edge)); 
        for(int i = 1; i <= m; i++){
            scanf("%d%d%d", &a, &b, &c);
            edge[a][b] = edge[b][a] = c;        
        }
        prim();
        printf("Scenario #%d:\n", k++);
        printf("%d\n\n", ans);
    }

    return 0;
}

 

posted @ 2017-11-07 22:26  楼主好菜啊  阅读(615)  评论(0编辑  收藏  举报