11.3.4 例題11-5 UVA 247 Audiophobia(兩點間最大權最小_floyd()變形)

來源:互聯網
上載者:User

標籤:

題目大意:

  給你一個n個節點,m條邊的圖,然後,給出q個詢問,讓你找出一條從u到v的路徑中所經過的最大權值最小的路徑權值。

解題思路:

  直接使用floyd的變形來做。

  e[i][j] = min(e[i][j],e[i][k]+e[k][j])---->e[i][j] = min(e[i][j],max(e[i][k],e[k][j]));

代碼:

# include<cstdio># include<iostream>using namespace std;# define MAX 123# define inf 99999999int e[MAX][MAX];int n,m,q;void init(){    for ( int i = 1;i <=n ;i++ )    {        for ( int j = 1;j <= n;j++ )        {            if ( i==j )                e[i][j] = 0;            else                e[i][j] = inf;        }    }}void floyd(){    for ( int k = 1;k <= n;k++ )    {        for ( int i = 1;i <= n;i++ )        {            for ( int j = 1;j <= n;j++ )            {                if ( e[i][k]<inf&&e[k][j]<inf )                e[i][j] = min(e[i][j],max(e[i][k],e[k][j]));            }        }    }}void print(){    for ( int i = 1;i <= n;i++ )    {        for ( int j = 1;j <= n;j++ )        {            cout<<e[i][j]<<" ";        }        cout<<endl;    }}int main(void){    int icase = 0;    while ( scanf("%d%d%d",&n,&m,&q)!=EOF )    {        if ( n==0 )            break;        init();        while ( m-- )        {            int a,b,c; scanf("%d%d%d",&a,&b,&c);            e[a][b] = min(e[a][b],c);            e[b][a] = c;        }        if (icase)            puts("");        printf("Case #%d\n",++icase);        floyd();       // print();        while ( q-- )        {            int t1,t2; scanf("%d%d",&t1,&t2);            int ans = e[t1][t2];            if (ans!=inf)                printf("%d\n",ans);            else                printf("no path\n");        }    }    return 0;}

  

11.3.4 例題11-5 UVA 247 Audiophobia(兩點間最大權最小_floyd()變形)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.