#include <bits/stdc++.h>

using namespace std;

const int N = 100010, M = 200020;
int n, m, p[N];

struct E
{
int a, b, c;
bool operator < (const E& w)
{
return c < w.c;
}
}e[M];

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

int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i ++) p[i] = i;
int res = 0, cnt = 0;
for(int i = 0; i < m; i ++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
e[i] = {a, b, c};
}
sort(e, e + m);
for(int i = 0; i < m; i ++)
{
int a = e[i].a, b = e[i].b, w = e[i].c;
if(find(a) != find(b)) p[p[a]] = p[b], cnt ++, res += w;
}
if(cnt != n - 1) puts("impossible");
else printf("%d\n", res);
return 0;
}


#include <iostream>
#include <cstring>

using namespace std;

const int N = 510, INF = 0x3f3f3f3f;
int g[N][N], dist[N],n, m;
bool st[N];

int prim()
{
int res = 0;
memset(dist, 0x3f, sizeof dist);
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;

st[t] = true;
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]);
}
return res;
}

int main()
{
memset(g, 0x3f, sizeof g);
cin >> n >> m;
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;
}



floyd算法，三重循环，内层一定是d[i][j] 和 d[i][k] + d[k][j],的最小值，因为是i到j的距离。

#include <iostream>
#include <cstring>

using namespace std;

const int N = 210;
int d[N][N], n, m, k;

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 >> k;
memset(d, 0x3f, sizeof d);
for(int i = 1; i <= n; i ++) d[i][i] = 0;
while(m --)
{
int a, b, c;
cin >> a >> b >> c;
d[a][b] = min(d[a][b], c);
}
floyd();
while(k --)
{
int a, b;
cin >> a >> b;
if(d[a][b] > 0x3f3f3f3f / 2) puts("impossible");
else cout << d[a][b] << endl;
}
return 0;
}


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

using namespace std;

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

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

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

while(q.size())
{
auto 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])
{
cnt[j] = cnt[t] + 1;
dist[j] = dist[t] + w[i];
if(cnt[j] >= n) return true;
if(!st[j])
{
st[j] = true;
q.push(j);
}
}
}
}
return false;
}

int main()
{
memset(h, -1, sizeof h);
cin >> n >> m;
while(m --)
{
int a, b, c;
cin >> a >> b >> c;
}
if(spfa()) puts("Yes");
else puts("No");
return 0;
}


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

using namespace std;

const int N = 100010, M = 200010;
int h[N], e[M], ne[M], idx, dist[N], w[N], n, m;
bool st[N];

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

int spfa()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
queue<int> q;
st[1] = true;
q.push(1);
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];
if(!st[j])
{
st[j] = true;
q.push(j);
}
}
}
}
if(dist[n] == 0x3f3f3f3f) return -1;
return dist[n];
}

int main()
{
memset(h, -1, sizeof h);
cin >> n >> m;
while(m --)
{
int a, b, c;
cin >> a >> b >> c;
}
int t = spfa();
if(t == -1) puts("impossible");
else cout << t << endl;
return 0;
}


#include <iostream>
#include <cstring>

using namespace std;

const int N = 510, M = 10010;
int dist[N], last[N], n, m, k;
struct edge
{
int a, b, c;
}edges[M];

void bellman_ford()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for(int i = 0; i < k; i ++)
{
memcpy(last, dist, sizeof dist);
for(int j = 0; j < m; j ++)
{
int a, b, w;
a = edges[j].a, b = edges[j].b, w = edges[j].c;
dist[b] = min(dist[b], last[a] + w);
}
}
}

int main()
{
cin >> n >> m >> k;
for(int i = 0; i < m; i ++)
{
int a, b, c;
cin >> a >> b >> c;
edges[i] = {a, b, c};
}
bellman_ford();
if(dist[n] > 0x3f3f3f3f / 2)  puts("impossible");
else cout << dist[n] << endl;
return 0;
}



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

using namespace std;
typedef pair<int, int> PII;

const int N = 1000010;
int n, m, h[N], ne[N], e[N], idx, w[N], dist[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}

int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, 1});
while(heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.second, dis = t.first;
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[ver] + w[i])
{
dist[j] = dis + w[i];
heap.push({dist[j], j});
}
}
}
if(dist[n] == 0x3f3f3f3f) return -1;
return dist[n];
}

int main()
{
memset(h, -1, sizeof h);
cin >> n >> m;
while(m --)
{
int a, b, c;
cin >> a >> b >> c;
}
cout << dijkstra() << endl;
return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 100010;
int q[N], d[N], h[N], e[N], ne[N], idx, n, m;
bool st[N];

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

void dfs(int u)
{
if(u > n)
{
for(int i = 1; i <= n ; i ++) printf("%d ", q[i]);
puts("");
return;
}
for(int i = 1; i <= n; i ++)
{
if(d[i] == 0 && !st[i])
{
q[u] = i;
for(int j = h[i]; j != -1; j = ne[j])
{
int t = e[j];
d[t] --;
}
st[i] = true;
dfs(u + 1);
st[i] = false;
for(int j = h[i]; j != -1; j = ne[j])
{
int t = e[j];
d[t] ++;
}
}
}
}

int main()
{
memset(h, -1, sizeof h);
cin >> n >> m;
while(m --)
{
int a, b;
cin >> a >> b;
}
dfs(1);
return 0;
}



#include <iostream>
#include <cstring>

using namespace std;

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

int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for(int i = 0; i < n - 1; i ++)
{
int t = -1;
for(int j = 1; j <= n; j ++)
{
if(!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
}
st[t] = true;
for(int j = 1; j <= n; j ++)
dist[j]  = min(dist[j], dist[t] + g[t][j]);
}
if(dist[n] == 0x3f3f3f3f) return -1;
return dist[n];
}

int main()
{
memset(g, 0x3f, sizeof g);
cin >> n >> m;
while(m --)
{
int a, b, c;
cin >> a >> b >> c;
g[a][b] = min(g[a][b], c);
}
cout << dijkstra() << endl;
return 0;
}


#include <iostream>
#include <cstring>

using namespace std;

const int N = 100010, M = 200020;
int h[N], e[M], ne[M], q[N], d[N], idx, n, m;

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

bool topsort()
{
int hh = 0, tt = -1;
for(int i = 1; i <= n; i ++)
{
if(!d[i]) q[++ tt] = i;
}
while(hh <= tt)
{
int t = q[hh ++];
for(int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if(-- d[j] == 0) q[++ tt] = j;
}
}
return tt == n - 1;
}

int main()
{
memset(h, -1, sizeof h);
cin >> n >> m;
while(m --)
{
int a, b;
cin >> a >> b;