HDU 4370 0 or 1(巧妙思路轉化,0/1規劃,最短路SPFA)

來源:互聯網
上載者:User

題目大意:給出一個n×n的矩陣,求一個n×n的只含有0和1的矩陣,使得兩個矩陣位置對應的元素相乘後和最小。

思路:巧妙的思路,把題目所給的三個條件以圖論的角度考慮可以轉換成一個求最短路徑的問題。
通過這題瞭解到了一個新的問題–“0/1規劃”。

各種題解:
官方題解
kuangbin
cxlove

Code:

/*   W          w           w        mm          mm             222222222       7777777777777    *//*    W        w w         w        m  m        m  m          222        22              7777    *//*    w        w w         w        m  m        m  m                     22              777     *//*     w      w   w       w        m    m      m    m                    22              77      *//*     w      w    w      w        m    m      m    m                 222                77      *//*      w    w      w    w        m      m    m      m              222                  77      *//*      w    w      w    w        m      m    m      m            222                    77      *//*       w  w        w  w        m        m  m        m         222                      77      *//*       w  w        w  w        m        m  m        m      222                         77      *//*        ww          ww        m          mm          m     222222222222222             77      *///#pragma comment(linker, "/STACK:102400000,102400000")//C++//int size = 256 << 20; // 256MB//char *p = (char*)malloc(size) + size;//__asm__("movl %0, %%esp\n" :: "r"(p));//G++#include<set>#include<map>#include<queue>#include<stack>#include<ctime>#include<deque>#include<cmath>#include<vector>#include<string>#include<cctype>#include<cstdio>#include<cstdlib>#include<cstring>#include<sstream>#include<iostream>#include<algorithm>#define REP(i,s,t) for(int i=(s);i<=(t);i++)#define REP2(i,t,s) for(int i=(t);i>=s;i--)using namespace std;typedef long long ll;typedef unsigned long long ull;typedef unsigned long ul;int n;const int MAXN=305;const int INF=0x3f3f3f3f;int path[MAXN][MAXN];bool vis[MAXN];//在隊列標誌int dist[MAXN];void SPFA(int start,int n){    memset(vis,false,sizeof(vis));    queue<int>Q;    for(int i=1; i<=n; i++)    {        if(i==start)        {            dist[i]=INF;        }        else        {            dist[i]=path[start][i];            Q.push(i);            vis[i]=1;        }    }    while(!Q.empty())    {        int u=Q.front();        Q.pop();        vis[u]=0;        REP(i,1,n)        {            if(dist[i]>dist[u]+path[u][i])            {                dist[i]=dist[u]+path[u][i];                if(!vis[i])                {                    vis[i]=1;                    Q.push(i);                }            }        }    }}int main(){#ifdef ONLINE_JUDGE#else    freopen("test.in","r",stdin);#endif    while(~scanf("%d",&n))    {        REP(i,1,n)        {            REP(j,1,n)            {                int c;                scanf("%d",&path[i][j]);            }        }        SPFA(1,n);        int ans=dist[n];        int c1=dist[1];        SPFA(n,n);        int c2=dist[n];        printf("%d\n",min(ans,c1+c2));    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.