题解 T28305 【yizimi的旅游景点】
生活随笔
收集整理的這篇文章主要介紹了
题解 T28305 【yizimi的旅游景点】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接這里!!!
AC軍團(tuán)月賽題目地址這里!!!正比例函數(shù)的旅游景點(diǎn)
題目翻譯
我依然不會(huì)告訴你題目的難度有一半是讀題
其實(shí)就是給定一個(gè)圖中一部分點(diǎn),給定一部分邊,先讓你求這之中的最小生成樹,然后再這個(gè)樹的基礎(chǔ)上求關(guān)于所有點(diǎn)和所有邊的最大生成樹。
好用的最小生成樹模板
看到這里我覺得大部分dalao就可以喊著“這出題人語(yǔ)文真差而且這道題真水”去寫代碼秒題了
如果您還往下看,只有兩種可能:
1.yizimi的語(yǔ)文太菜了,題目翻譯還是看不懂2.不會(huì)寫代碼誒好,基本上就沒別的彎路,上代碼:
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define go(i, j, n, k) for (int i = j; i <= n; i += k) #define fo(i, j, n, k) for (int i = j; i >= n; i -= k) #define rep(i, x) for (int i = h[x]; i; i = e[i].nxt) #define mn 20020 #define mm 200010 #define inf 1 << 30 #define ll long long #define ld long double #define fi first #define se second #define root 1, n, 1 #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 #define bson l, r, rt inline int read() {int f = 1, x = 0;char ch = getchar();while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f; } inline void write(int x) {if (x < 0)putchar('-'), x = -x;if (x > 9)write(x / 10);putchar(x % 10 + '0'); } //This is AC head above... int n, m; int ans, tot, cnt; bool b[mn]; struct edge {int u, v, w, p;bool used; } e[mm]; inline bool cmp1(edge a, edge b) {return a.w < b.w; } inline bool cmp2(edge a, edge b) {return a.w > b.w; } int father[mn]; inline int findx(int x) {return father[x] == x ? x : father[x] = findx(father[x]); } inline void mergex(int x, int y)//隨機(jī)合并大法好 {int xx = findx(x);int yy = findx(y);if (xx == yy)return;srand((unsigned)time(NULL));//隨機(jī)合并if (rand() % 2){father[xx] = yy;}else{father[yy] = xx;} } inline void Krus()//最小生成樹 {go(i, 1, n, 1)father[i] = i;sort(e + 1, e + m + 1, cmp1);go(i, 1, m, 1){if (b[e[i].u] && b[e[i].v] && findx(e[i].u) != findx(e[i].v) && e[i].p == 1 && !e[i].used){ans += e[i].w;mergex(e[i].u, e[i].v);e[i].used = true;cnt++;}if (cnt == tot - 1)return;} } inline void Krub()//最大生成樹 {sort(e + 1, e + m + 1, cmp2);go(i, 1, m, 1){if (findx(e[i].u) != findx(e[i].v) && !e[i].used){ans += e[i].w;mergex(e[i].u, e[i].v);e[i].used = true;cnt++;}if (cnt == n - 1)return;} } int main() {//freopen("002.in", "r", stdin);//freopen("002.out", "w", stdout);n = read(), m = read();go(i, 1, n, 1){b[i] = read();//cin >> b[i];//scanf("%d", &b[i]);if (b[i])tot++;}go(i, 1, m, 1){e[i].u = read(), e[i].v = read(), e[i].w = read(), e[i].p = read(), e[i].w *= e[i].p, e[i].used = false;//cin >> e[i].u >> e[i].v >> e[i].w >> e[i].p;//scanf("%d%d%d%d", &e[i].u, &e[i].v, &e[i].w, &e[i].p);//e[i].w *= e[i].p, e[i].used = false;}//這里你可以試試cin,保證慢死Krus();if (cnt != tot - 1){cout << -1;return 0;}int ans1 = ans;Krub();if (cnt != n - 1){cout << -1;return 0;}cout << ans1 << " " << ans - ans1;return 0; }有些大佬看到這里可能會(huì)說(shuō):
明明我寫對(duì)了,為什么會(huì)T?
我只是想說(shuō):
dalao您先看看時(shí)限!
這個(gè)題是300ms,如果你用cin大約是600ms(部分點(diǎn)),不開O2都有可能T掉。
這就是這個(gè)題的最大的坑
讀入優(yōu)化大法好
轉(zhuǎn)載于:https://www.cnblogs.com/yizimi/p/10056230.html
總結(jié)
以上是生活随笔為你收集整理的题解 T28305 【yizimi的旅游景点】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 学习Linux课程第十二天
- 下一篇: JAVA读取文件操作时路径的斜杠问题