生活随笔
收集整理的這篇文章主要介紹了
51nod 3 * problem
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1640
題意:
一張無向圖
在最小化最大邊后求最大邊權(quán)和
Slove:
sort
最小生成樹
倒敘最大生成樹
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <
string>
using namespace std;#define LL long long
#define gc getchar()
inline int read() {
int x =
0, f =
1;
char c =
gc;
while(c <
'0' || c >
'9') {
if(c ==
'-') f = -
1; c =
gc;}
while(c >=
'0' && c <=
'9') x = x *
10 + c -
'0', c = gc;
return x *
f;}
inline LL read_LL() {LL x =
0;
char c = gc;
while(c <
'0' || c >
'9') c =
gc;
while(c >=
'0' && c <=
'9') x = x *
10 + c -
'0', c = gc;
return x;}
#undef gc
const int N = 1e5 +
10;int fa[N];
int A[N <<
1], U[N <<
1], V[N <<
1], W[N <<
1];
int n, m;bool Cmp(
int a,
int b) {
return W[a] <
W[b];}int Get(
int x) {
return fa[x] == x ? x : fa[x] =
Get(fa[x]);}void Minst(
int &
R) {for(
int i =
1; i <= n; i ++) fa[i] =
i;int js =
0;for(
int i =
1; i <= m; i ++
) {int fu = Get(U[A[i]]), fv =
Get(V[A[i]]);if(fu !=
fv) {fa[fu] =
fv;js ++
;}if(js == n -
1) {R =
i;while(W[A[R +
1]] == W[A[i]]) R ++
;return ;}}
}inline long long Maxst(
int R) {for(
int i =
1; i <= n; i ++) fa[i] =
i;int js =
0;long long ret =
0;for(
int i = R; i >=
1; i --
) {int fu = Get(U[A[i]]), fv =
Get(V[A[i]]);if(fu !=
fv) {fa[fu] =
fv;ret +=
W[A[i]];js ++
;}if(js == n -
1)
return ret;}
}int main() {n = read(), m =
read();for(
int i =
1; i <= m; i ++) A[i] = i, U[i] = read(), V[i] = read(), W[i] =
read();sort(A +
1, A + m +
1, Cmp);int R;Minst(R);cout <<
Maxst(R);return 0;
} 1649
由于 1 - n 之間一定存在一種直接相連的道路
判斷哪種直接相連
跑另外一種的最短路
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <
string>
using namespace std;#define LL long long
#define gc getchar()
inline int read() {
int x =
0;
char c = gc;
while(c <
'0' || c >
'9') c =
gc;
while(c >=
'0' && c <=
'9') x = x *
10 + c -
'0', c = gc;
return x;}
inline LL read_LL() {LL x =
0;
char c = gc;
while(c <
'0' || c >
'9') c =
gc;
while(c >=
'0' && c <=
'9') x = x *
10 + c -
'0', c = gc;
return x;}
#undef gc
const int N =
410, oo =
999999999;int Map[N][N], Bmap[N][N];
int n, m;int main() {n = read(), m =
read();for(
int i =
1; i <= n; i ++)
for(
int j =
1; j <= n; j ++) Map[i][j] =
oo;for(
int i =
1; i <= n; i ++) Map[i][i] =
0;for(
int i =
1; i <= n; i ++)
for(
int j =
1; j <= n; j ++) Bmap[i][j] =
oo;for(
int i =
1; i <= n; i ++) Bmap[i][i] =
0;for(
int i =
1; i <= m; i ++
) {int u = read(), v =
read();Map[u][v] = Map[v][u] =
1;}if(Map[
1][n] ==
1) {for(
int i =
1; i <= n; i ++
) for(
int j =
1; j <= n; j ++
) {if(i == j)
continue;if(Map[i][j] == oo) Bmap[i][j] =
1;}for(
int k =
1; k <= n; k ++
)for(
int i =
1; i <= n; i ++
)for(
int j =
1; j <= n; j ++
)Bmap[i][j] = min(Bmap[i][j], Bmap[i][k] +
Bmap[k][j]);if(Bmap[
1][n] == oo) cout << -
1;else cout << Bmap[
1][n];} else {for(
int k =
1; k <= n; k ++
)for(
int i =
1; i <= n; i ++
)for(
int j =
1; j <= n; j ++
)Map[i][j] = min(Map[i][j], Map[i][k] +
Map[k][j]);if(Map[
1][n] == oo) cout << -
1;else cout << Map[
1][n];}return 0;
} 1535
圖是樹的充要條件
$m = n - 1$ && 圖聯(lián)通
由于題目無自環(huán)
所以不存在二元環(huán)
并且若 $m >= n - 1$
則圖聯(lián)通
此時若 $m = n$
那么就會存在且只存在一個三元環(huán)(或更大)
因此只需判斷 $n = m$ 即可
if(n == m) cout <<
"FHTAGN!";
else cout <<
"NO";
?
轉(zhuǎn)載于:https://www.cnblogs.com/shandongs1/p/9591918.html
總結(jié)
以上是生活随笔為你收集整理的51nod 3 * problem的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。