HUSTzyk

361

NUC-CYJ

lsz_
Byte_1
guluguluboy

kerman0411
ltss
Nobody_Z
Andy2035
Ethanyyc
xiuzhiyuan
smr666
cjlworld

HUSTzyk
9天前

#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 100010;

int n;
struct Range
{
int l, r;
bool operator< (const Range &W)
{
return l < W.l;
}
} ranges[N];

int main()
{
cin >> n;

for(int i = 0; i < n; i ++ )
{
int l, r;
cin >> l >> r;
ranges[i] = {l, r};
}

sort(ranges, ranges + n);

priority_queue<int, vector<int>, greater<int>> heap;
for(int i = 0; i < n; i ++ )
{
auto r = ranges[i];
if(heap.empty() || heap.top() >= r.l) heap.push(r.r);
else
{
int t = heap.top();
heap.pop();
heap.push(r.r);
}
}

printf("%d\n", heap.size());

return 0;
}


HUSTzyk
10天前

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int n;
struct Range
{
int l, r;
bool operator< (const Range &W)const
{
return r < W.r;
}
} ranges[N];

int main()
{
cin >> n;
for(int i = 0; i < n; i ++ )
{
int l, r;
cin >> l >> r;
ranges[i] = {l, r};
}

sort(ranges, ranges + n);

int res = 0, ed = -2e9;
for(int i = 0; i < n; i ++ )
{
if(ranges[i].l > ed)
{
ed = ranges[i].r;
res ++ ;
}
}

cout << res << endl;

return 0;
}


HUSTzyk
10天前

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int n;
struct Range
{
int l, r;
bool operator< (const Range &W)const
{
return r < W.r;
}
}

int main()
{
cin >> n;

for(int i = 0; i < n; i ++ )
{
int l, r;
cin >> l >> r;
ranges[i] = {l, r};
}

sort(ranges, ranges + n);

int res = 0, ed = -2e9
for(int i = 0; i < n; i ++ )
{
if(ranges[i].l > ed)
{
res ++ ;
ed = ranges[i].r;
}
}

cout << res << endl;

return 0;
}


HUSTzyk
10天前

Kruskal算法求最小生成树(带思路版)

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 200010;

int n, m;
int p[N];

struct Edge
{
int a, b, w;

bool operator< (const Edge &W)const
{
return w < W.w;
}
} edges[N];

int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}

int main()
{
cin >> n >> m;

for(int i = 0; i < m; i ++ )
{
int a, b, w;
cin >> a >> b >> w;
edges[i] = {a, b, w};
}

sort(edges, edges + m); //将所有边按权重从小到大排序

for(int i = 1; i <= n; i ++ ) p[i] = i;

int res = 0, cnt = 0;
for(int i = 0; i < m; i ++ ) //枚举每条边
{
int a = edges[i].a, b = edges[i].b, w = edges[i].w;

a = find(a), b = find(b);
if(a != b)
{
p[a] = b; //若点a, b不连通，则加入集合中
res += w;
cnt ++ ;
}
}

if(cnt < n - 1) puts("impossible");
else printf("%d\n", res);

return 0;
}


HUSTzyk
10天前

Kruskal算法求最小生成树

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 200010;

int n, m;
int p[N];

struct Edge
{
int a, b, w;

bool operator< (const Edge &W)const
{
return w < W.w;
}
} edges[N];

int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}

int main()
{
cin >> n >> m;

for(int i = 0; i < m; i ++ )
{
int a, b, w;
cin >> a >> b >> w;
edges[i] = {a, b, w};
}

sort(edges, edges + m);

for(int i = 1; i <= n; i ++ ) p[i] = i;

int res = 0, cnt = 0;
for(int i = 0; i < m; i ++ )
{
int a = edges[i].a, b = edges[i].b, w = edges[i].w;

a = find(a), b = find(b);
if(a != b)
{
p[a] = b;
res += w;
cnt ++ ;
}
}

if(cnt < n - 1) puts("impossible");
else printf("%d\n", res);

return 0;
}


HUSTzyk
10天前

Prim算法

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 510, INF = 0x3f3f3f3f;

int n, m;
int g[N][N];
int dist[N];
bool st[N];

int prim()
{
memset(dist, 0x3f, sizeof dist);

int res = 0;
for(int i = 0; i < n; i ++ )
{
int t = -1;
for(int j = 1; j <= n; j ++ )
if(!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;

if(i && dist[t] == INF) return INF;
if(i) res += dist[t];

for(int j = 1; j <= n; j ++ ) dist[j] = min(dist[j], g[t][j]);

st[t] = true;
}

return res;
}

int main()
{
cin >> n >> m;

memset(g, 0x3f, sizeof g);

while(m -- )
{
int a, b, c;
cin >> a >> b >> c;
g[a][b] = g[b][a] = min(g[a][b], c);
}

int t = prim();

if(t == INF) puts("impossible");
else printf("%d\n", t);

return 0;
}


HUSTzyk
12天前
AcSaber 可真是个好东西！😆

HUSTzyk
12天前

HUSTzyk
12天前

Floyd算法求最短路

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 210, INF = 1e9;

int n, m, Q;
int d[N][N];

void floyd()
{
for(int k = 1; k <= n; k ++ )
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ )
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main()
{
cin >> n >> m >> Q;

for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ )
if(j == i) d[i][j] = 0;
else d[i][j] = INF;

while(m -- )
{
int a, b, w;
cin >> a >> b >> w;

d[a][b] = min(d[a][b], w);
}

floyd();

while(Q -- )
{
int a, b;
cin >> a >> b;

if(d[a][b] > INF / 2) puts("impossible");
else printf("%d\n", d[a][b]);
}

return 0;
}



HUSTzyk
12天前

spfa算法判断负环

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 100010;

int n, m;
int h[N], e[N], w[N], ne[N], idx;
int dist[N], cnt[N];
bool st[N];

void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

int spfa()
{
queue<int> q;

for(int i = 1; i <= n; i ++ )
{
st[i] = true;
q.push(i);
}

while(q.size())
{
int t = q.front();
q.pop();

st[t] = false;

for(int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;

if(cnt[j] > n) return true;
if(!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}

return false;
}

int main()
{
cin >> n >> m;

memset(h, -1, sizeof h);

while(m -- )
{
int a, b, c;
cin >> a >> b >> c;