poj1062 昂貴的聘禮 最短路

來源:互聯網
上載者:User

PS:中文題太爽了。。

題意:中文題我就不寫題意了。。。

分析:我感覺要注意兩點。。。1.注意等級限制,我本來以為只有在相鄰的兩個人物中有等級的限制,其實在你需要交換的所有人都有等級的限制。。。2.建圖要建有向圖。。不是無向圖。。我因為這個哇了半天。。。

具體做法就是,建圖,然後枚舉dijk

代碼:


#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;#define inf (0x3f3f3f3f)const int maxn = 105;int m, n;struct Goods{int p, l, v;int num[maxn];int val[maxn];}goods[maxn];int map[maxn][maxn];int dijk(int x){int i, j;int level = goods[x].l;int cost[maxn];bool vis[maxn];memset(cost, inf, sizeof(cost));memset(vis, false, sizeof(vis));for (i=0; i<=n; i++){if (goods[i].l - level <= m && goods[i].l >= level)cost[i] = map[0][i];}vis[0] = true;for (i=0; i<n; i++){int min = inf, imin;for (j=0; j<=n; j++){if (!vis[j] && cost[j] < min){imin = j;min = cost[j];}}if (min == inf)break;vis[imin] = true;for (j=0; j<=n; j++){if (goods[j].l - level <= m && goods[j].l >= level)if (cost[j] > cost[imin] + map[imin][j])cost[j] = cost[imin] + map[imin][j];}}return cost[1];}int main(){while (scanf("%d %d", &m, &n) != EOF){memset(goods, 0, sizeof(goods));memset(map, inf, sizeof(map));int i, j;for (i=1; i<=n; i++){scanf("%d %d %d", &goods[i].p, &goods[i].l, &goods[i].v);for (j=0; j<goods[i].v; j++){scanf("%d %d", &goods[i].num[j], &goods[i].val[j]);}map[0][i] = goods[i].p;}for (i=1; i<=n; i++){for (j=0; j<goods[i].v; j++){if (abs(goods[goods[i].num[j]].l - goods[i].l) <= m){ if (map[goods[i].num[j]][i] > goods[i].val[j]){map[goods[i].num[j]][i] = goods[i].val[j];}}}}int min = inf;for (i=1; i<=n; i++){int tmp = dijk(i);if (tmp < min)min = tmp;}printf("%d\n", min);}return 0;}

網上大牛的測試資料:

測試資料1:1 410000 3 22 80003 50001000 2 14 2003000 2 14 20050 2 05250測試資料2:1 510000 3 42 30003 20004 20005 90008000 2 33 50004 20005 70005000 1 02000 4 15 190050 1 04000測試資料3:3 810000 3 62 30003 20004 20005 90007 10008 50088000 2 33 50004 20005 70005000 1 16 10002000 4 15 190050 1 05000 1 17 40072000 4 15 190080 3 02950測試資料4:1 101324 0 01234 0 0255 0 067 0 056 0 02134 0 0456 0 02345 0 067 0 06436 0 01324測試資料5:1 410000 3 22 13 31000 2 24 13 11000 3 14 2100 4 0105測試資料6:3 510000 3 42 30003 20004 20005 90008000 2 33 50004 20005 70005000 1 02000 4 15 190050 1 03950測試資料7:0 510000 3 42 30003 20004 20005 90008000 2 33 50004 20005 70005000 4 02000 3 15 190050 2 03950

聯繫我們

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