poj 1062 昂貴的聘禮 解題報告

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   div   

題目連結:http://poj.org/problem?id=1062

      這一題只要想到如何建圖,就不太難解決了。假設對於編號為 i 的物品,如果它得到物品 j 後價格從 pricei 降低到 pricej 的話,就用一個cost[i][j] = pricej。也就是從物品 i 到物品 j 連一條有向邊。每一個編號的物品都這樣處理,然後套用dijk 演算法,求出從每個點出發的最短路,最終最小的那個就是答案。考慮到等級限制,別人可以跟酋長接觸的前提是這個人的等級在 [ level 酋長-m  ~ level酋長+m ] 範圍內。

       這題可以算得上比較靈活的 最短路。

     

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 using namespace std; 6  7 const int INF = 1e9; 8 const int N = 100 + 10; 9 int dist[N], vis[N];10 int level[N], cost[N][N];11 int n, m;12 13 int dijkstra(int cost[][N], int p)14 {15     memset(vis, 0, sizeof(vis));16     for (int i = 1; i <= n; i++)17         dist[i] = cost[0][i];18     dist[0] = 0, vis[0] = 1;19     for (int i = 1; i <= n; i++)20     {21         int u;22         int maxx = INF;23         for (int j = 1; j <= n; j++)24         {25             if (dist[j] < maxx && !vis[j])26                 maxx = dist[u=j];27         }28         vis[u] = 1;29         for (int j = 0; j <= n; j++)30         {31             if (!vis[j] && dist[j] > dist[u]+cost[u][j] && level[u] >= p && level[u] <= p+m)32                 dist[j] = dist[u] + cost[u][j];33         }34     }35     return dist[1];36 }37 38 int main()39 {40     while (scanf("%d%d", &m, &n) != EOF)41     {42         int P, L, X;43         int id, price;44         for (int i = 0; i <= n; i++)45         {46             for (int j = 0; j <= n; j++)47                 cost[i][j] = (i == j ? 0 : INF);48         }49         for (int i = 1; i <= n; i++)50         {51             scanf("%d%d%d", &P, &L, &X);52             cost[0][i] = P;53             level[i] = L;54             for (int j = 0; j < X; j++)55             {56                 scanf("%d%d", &id, &price);57                 cost[id][i] = price;58             }59         }60         int ans = INF;61         for (int i = level[1]-m; i <= level[1]; i++)62             ans = min(ans, dijkstra(cost, i));63         printf("%d\n", ans);64     }65     return 0;66 }

 

   

聯繫我們

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