zoj 1586 QS Network,zoj1586qsnetwork
連結:zoj 1586
題意:若兩個QS之間要想連網,除了它們間網線的費用外,兩者都要買適配器,
求使所有的QS都能連網的最小費用
分析:這個除了邊的權值外,頂點也有權值,因此要想求最小价值,必須算邊及頂點的權值和
#include<cstdio>#include<algorithm>using namespace std;int f[1010],n,m;struct stu{ int a,b,c;}t[500100];int cmp(struct stu x,struct stu y){ return x.c<y.c;}int find(int x){ if(x!=f[x]) f[x]=find(f[x]); return f[x];}int krus(){ int i,k=0,s=0,x,y; for(i=1;i<m;i++){ x=find(t[i].a); y=find(t[i].b); if(x!=y){ s+=t[i].c; k++; if(k==n-1) break; f[x]=y; } } return s;}int main(){ int T,i,j,a[1010],c,s=0; scanf("%d",&T); while(T--){ scanf("%d",&n); for(i=1;i<=n;i++){ f[i]=i; scanf("%d",&a[i]); } m=1; for(i=1;i<=n;i++) for(j=1;j<=n;j++){ scanf("%d",&c); if(j<i){ t[m].a=i; t[m].b=j; t[m++].c=c+a[i]+a[j]; //計算邊和頂點的權值和 } } sort(t+1,t+m,cmp); s=krus(); printf("%d\n",s); } return 0;}
一道DP經典題最好自創帶標程
zju搜尋dp題目總結收藏
|
搜尋:
50道搜尋題
1002 Fire Net -ok
1004 Anagrams by Stack -ok
1005 Jugs -ok
1008 Gnome Tetravex -ok but not ac
1091 Knight Moves -ok
1101 Gamblers -ok
1204 Additive equations
1221 Risk
1230 Legendary Pokemon
1249 Pushing Boxes
1364 Machine Schedule
1368 BOAT
1406 Jungle Roads ---ok
1411 Anniversary
1453 Surround the Trees 凸包
1516 Uncle Tom's Inherited Land ---ok
1525 Air Raid 算網路流問題可能更合適一點
1586 QS Network
1602 Multiplication Puzzle dp
1649 Rescue
1671 Walking Ant----ok
1711 Sum It Up dfs----ok
1901 A Star not a Tree? 說他搜尋可能有點爭議(不管了)
1940 Dungeon Master----ok
2100 Seeding ----ok
2110 Tempter of the Bone
2140 Ball
(27道)
上面的題可能比較簡單,就當練練基本演算法吧
===============================================================================
下面的題沒有容易做的。
難題&經典
1003 Crashing Balloon
1015 Fishing Net 完美圖
1144 Robbery
1149 Dividing up
1161 Gone Fishing
1197 Sorting Slides
1217 Eight
1228 Farewell, My Friend
1237 Fans and Gems
1455 Schedule Problem
1456 Minimum Transport Cost 圖論 最短路徑 要儲存路徑
1492 Maximum Clique 圖論 經典演算法--最大團
1600 Market Place
1605 One-way Traffic
1568 WishingBone's Room Plan
1742 Gap 至今一點想法都沒有,難!!!
1743 Concert Hall Scheduling
1827 The Game of 31 博弈
1855 Maze
1903 Jogging Trails 中國郵路問題
1909 Square 經典的dfs.
2064 Bomberman - Just Search! 經典!
2094 Max Angle 計算幾何+博弈
2125 Rocket Mania
2126 Rocket Mania Plus
2127 Zuma
2128 Seven Seas
2129 Mummy Maze
2142 Light The Square
(24道)
===========================================
dp
50道dp的題
1499 Increasing Sequenc......餘下全文>>
ZOJ - 1649優先順序隊列的廣度優先演算法OJ沒通過?
你有點藝高人膽大啊。。
竟然把a這個char數組重用了,用來記錄sum
存在問題:
既然是char數組,那麼最大值就是256,可能不足以記錄sum的大小
如果某刻a裡面存放的sum恰好等於'a', 'r', '.', '#', 那麼你就會無法分辨那個地方是走過後記錄的sum,還是沒走的'a', 'r', '.', '#'
具體修改參照我的注視
#include<iostream>#include<string>#include<queue>using namespace std;int n, m, i, j, sum = 0;int rx, ry;char a[205][205];int aa[205][205]; // 新增加aa,代替原來a數組存放sumstring s;struct State{int x, y;State(int x, int y){this->x = x;this->y = y;}};struct cmp{ bool operator () (State a, State b){return aa[a.x][a.y]>aa[b.x][b.y]; // 從aa中取值比較}};priority_queue<State, vector<State>, cmp> qs; bool find(State sStart){int dir[4][2] = { { 1, 0 }, { 0, -1 }, { -1, 0 }, { 0, 1 } };qs.push(sStart);aa[sStart.x][sStart.y] = sum; //設定已訪問標誌,並用sum記錄走到這步的最少時間while (!qs.empty()){State state = qs.top();qs.pop();for (int i = 0; i < 4; i++) {int xx = state.x + dir[i][0];int yy = state.y + dir[i][1];if (aa[xx][yy] != -1) continue; // 如果之前走過,則跳過if (a[xx][yy] == 'a'){ sum = aa[xx - dir[i][0]][yy - dir[i][1]] + 1; // 用aa代替return true;}if (a[xx][yy] == '.'){ aa[xx][yy] = aa[xx - dir[i][0]][yy - dir[i][1]] + 1; // 用aa代替qs.push(State(xx, yy));}if (a[xx][yy] == 'x'){ aa[xx][yy] = aa[xx - dir[i][0]][yy - dir[i][1]] + 2; // 用aa代替qs.push(State(xx, yy));}}}return false;}in......餘下全文>>