Ultraviolet A 544 heavy cargo (variant of the minimal spanning tree)

Source: Internet
Author: User

This question is the maximum Spanning Tree and the minimum weight edge on the bottleneck.

The Code is as follows:

#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <map>#include <iostream>using namespace std;const int N = 210;const int INF = -1;const int INFB = 100000;int n, r, g[N][N], p[N], f[N][N], d[N];bool mark[N];string S, T;void prim() {    bool vis[N];    memset(vis, 0, sizeof(vis));    for ( int i = 1; i <= n; ++i ) d[i] = INF, p[i] = INF;    d[1] = 0;    int mi, v, ans = 0;    for ( int u = 0; u < n; ++u ) {        mi = INF;        for ( int i = 1; i <= n; ++i ) if ( !vis[i] && d[i] > mi ) v = i, mi = d[i];        ans += mi;        //cout << mi << endl;        vis[v] = true;        for ( int i = 1; i <= n; ++i ) if ( !vis[i] && d[i] < g[v][i] )  p[i] = v, d[i] = g[v][i];     }    //cout << ans << endl;}void dfs( int v ) {    for ( int u = 1; u <= n; ++u )         if ( !mark[u] && p[u] == v ) {           mark[u] = true;           //cout << u << ' ';           for ( int x = 1; x <= n; ++x ) if ( mark[x] && x != u ){           //   cout << f[x][v] << ' ' << g[u][v] << endl;               f[x][u] = f[u][x] = min( f[x][v], g[u][v] );           }           dfs(u);        }} int main(){    int icase = 1;    while ( scanf("%d%d", &n, &r), !(!n&&!r) ) {        getchar();        map <string,int> mp;        int id = 1;        for ( int i = 1; i <= n; ++i ) for ( int j = 1; j <= n; ++j ) g[i][j] = INF;        mp.clear();        while ( r-- ) {            string s1, s2;            int w;            cin >> s1 >> s2 >> w;            if ( !mp[s1] ) mp[s1] = id++;            if ( !mp[s2] ) mp[s2] = id++;            int u = mp[s1], v = mp[s2];            g[u][v] = g[v][u] = w;        }        cin >> S >> T;        prim();        for ( int i = 1; i <= n; ++i ) for ( int j = i; j <= n; ++j ) f[i][j] = f[j][i] = INFB;        memset( mark, 0, sizeof(mark));        mark[1] = true;        dfs( 1 );        //for ( int i = 1; i <= n; printf("\n"),++i ) for ( int j = 1; j <= n; ++j ) cout << f[i][j] << ' ';        int u = mp[S], v = mp[T];        printf("Scenario #%d\n%d tons\n\n", icase++, f[u][v]);    }}         

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.