P1363 幻想迷宫
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1505;
bool a[MAXN][MAXN];
int visit[MAXN][MAXN][3];
int f[4][2] = {
{
1,0},{
-1,0},{
0,1},{
0,-1} };
int n, m;
int bx, by;
bool f1;
void dfs(int x, int y,int lx,int ly) {
if (f1)return;
if (visit[x][y][0] && (visit[x][y][1] != lx || visit[x][y][2] != ly)) {
f1 = 1;
return;
}
visit[x][y][0] = 1, visit[x][y][1] = lx, visit[x][y][2] = ly;
for (int i = 0;i < 4;i++) {
int xx = (x + f[i][0] + n) % n, yy = (y + f[i][1] + m) % m;
int lxx = lx + f[i][0], lyy = ly + f[i][1];
if (!a[xx][yy]) {
if (visit[xx][yy][1] != lxx || visit[xx][yy][2] != lyy || !visit[xx][yy][0])
dfs(xx, yy, lxx, lyy);
}
}
}
int main() {
ios::sync_with_stdio(false);
while (cin >> n >> m) {
f1 = 0;
memset(a, 0, sizeof(a));
memset(visit, 0, sizeof visit);
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
char ch;
cin >> ch;
if (ch == '#')a[i][j] = 1;
if (ch == 'S')bx = i, by = j;
}
}
dfs(bx, by, bx, by);
if (f1)cout << "Yes" << endl;
else cout << "No" << endl;
}
}
P1127 词链
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1005;
int n;
string s[MAXN];
int out[26], in[26];
int p=-1, q=-1;
vector<string>ans;
string res;
bool flag = false;
vector<string>edge[26];
map<string, int>m;
bool cmp(string a, string b) {
return a < b;
}
void dfs(int f,int num) {
if (num == n) {
int si = ans.size();
for (int i = 0;i < si;i++) {
res += ans[i];
res += '.';
}
res.pop_back();
flag = true;
return;
}
sort(edge[f].begin(), edge[f].end(),cmp);
int sii = edge[f].size();
for (int i = 0;i <sii;i++) {
if (m[edge[f][i]] > 0) {
m[edge[f][i]]--;
ans .push_back( edge[f][i]);
int len = edge[f][i].length();
dfs(edge[f][i][len - 1] - 'a', num + 1);
if (flag == false){
ans.pop_back();
m[edge[f][i]]++;
}
else return;
}
}
}
int main() {
cin >> n;
for (int i = 1;i <= n;i++) {
cin >> s[i];
m[s[i]]++;
int len = s[i].length();
int last = s[i][len - 1] - 'a';
int begin = s[i][0] - 'a';
in[last]++;
out[begin]++;
edge[begin].push_back(s[i]);
}
int num = 0;
for (int i = 0;i < 26;i++) {
if (in[i] != out[i]) {
num++;
if (in[i] - out[i] == 1)q = i;
if (in[i] - out[i] == -1)p = i;
}
}
if (num != 0 && num != 2) {
cout << "***" << endl;
}
else if (num == 0) {
int fir = 0;
while (in[fir] == 0)fir++;
dfs(fir, 0);
if (res != "")cout << res << endl;
else cout << "***" << endl;
}
else if (num==2&&p != -1 && q != -1) {
dfs(p, 0);
if (res != "")cout << res << endl;
else cout << "***" << endl;
}
else cout << "***" << endl;
}
P1629 邮递员送信
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1e3+5;
const int INF = 1e9 + 7;
int n, m;
int dis[MAXN];
bool ans[MAXN];
struct node {
int x, dis_x;
bool operator <(const node& b) const{
return dis_x>b.dis_x;
}
};
struct Edge {
int x, y, w;
};
vector<Edge>edge[MAXN][2];
int dj(int t) {
for (int i = 1;i <= n;i++) {
dis[i] = INF;
}
memset(ans, 0, sizeof ans);
priority_queue<node>q;
dis[1] = 0;
q.push({
1,0 });
while (!q.empty()) {
node now = q.top();
q.pop();
if (ans[now.x])continue;
ans[now.x] = true;
for (int i = 0;i < edge[now.x][t].size();i++) {
Edge e = edge[now.x][t][i];
if (ans[e.y])continue;
if (dis[e.y] > dis[e.x] + e.w) {
dis[e.y] = dis[e.x] + e.w;
q.push({
e.y,dis[e.y]});
}
}
}
int res = 0;
for (int i = 1;i <= n;i++) {
res += dis[i];
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1;i <= m;i++) {
int from, to, w;
cin >> from >> to >> w;
edge[from][0].push_back({
from,to,w });
edge[to][1].push_back({
to,from,w });
}
int res = 0;
res += dj(0);
res += dj(1);
cout << res << endl;
}
P1144 最短路计数
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1e6+5;
const int INF = 1e9 + 7;
const int mod = 100003;
int n, m;
int dis[MAXN];
int ans[MAXN];
bool vis[MAXN];
vector<int>edge[MAXN];
void bfs() {
queue<int>q;
q.push(1);
dis[1] = 0;
ans[1] = 1;
vis[1] = 1;
while (!q.empty()) {
int f = q.front();
q.pop();
for (int i = 0;i < edge[f].size();i++) {
int now = edge[f][i];
if (!vis[now]) {
vis[now] = 1;
dis[now] = dis[f]+1;
q.push(now);
}
if (dis[now] == dis[f] + 1)ans[now] = (ans[now] + ans[f]) % mod;
}
}
}
int main() {
cin >> n >> m;
for (int i = 1;i <= m;i++) {
int a, b;
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
bfs();
for (int i = 1;i <= n;i++) {
cout << ans[i]%mod << endl;
}
return 0;
}
P1462 通往奥格瑞玛的道路
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1e4+5;
const int INF = 2e9 + 7;
const int mod = 100003;
int n, m, b;
int dis[MAXN];
bool vis[MAXN];
struct Node {
int x, f,dis_x;
bool operator < (const Node& a)const {
return dis_x > a.dis_x;
}
}node[MAXN];
struct Edge {
int from, to, c;
};
vector<Edge>edge[5 * MAXN];
bool dj(int mf) {
memset(vis, 0, sizeof vis);
for (int i = 1;i <= n;i++)dis[i] = INF;
priority_queue<Node>q;
if (node[1].f > mf)return false;
q.push({
node[1].x,node[1].f,0 });
dis[1] = 0;
while (!q.empty()) {
Node now = q.top();
q.pop();
if (vis[now.x])continue;
if (now.dis_x > b)return false;
vis[now.x] = true;
if (now.x == n)return true;
for (int i = 0;i < edge[now.x].size();i++) {
Edge p = edge[now.x][i];
if (node[p.to].f > mf) {
vis[p.to] = true;continue; }
if (vis[p.to])continue;
if (dis[p.to] > dis[now.x] + p.c) {
dis[p.to] = dis[now.x] + p.c;
q.push({
p.to,node[p.to].f,dis[p.to] });
}
}
}
if (dis[n] <= b)
return true;
else return false;
}
int main() {
cin >> n >> m >> b;
int maxf = 0;
for (int i = 1;i <= n;i++) {
cin >> node[i].f;
node[i].x = i, node[i].dis_x = INF;
if (node[i].f > maxf)maxf = node[i].f;
}
for (int i = 1;i <= m;i++) {
int a, b, c;
cin >> a >> b >> c;
edge[a].push_back({
a,b,c });
edge[b].push_back({
b,a,c });
}
int l = 1,r = maxf;
if (!dj(maxf)) {
cout << "AFK" << endl; }
else {
while (l < r) {
int mid = (l + r) / 2;
if (dj(mid))r = mid;
else l = mid + 1;
}
cout << l << endl;
}
}
P3387【模板】缩点
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1e4+5;
const int INF = 2e9 + 7;
const int mod = 100003;
int n, m;
int w[MAXN];
int num[MAXN], low[MAXN];
int scc[MAXN],ans[MAXN];
int in[MAXN];
int dfn =0,cnt= 0;
stack<int>st;
vector<int>edge[MAXN];
void dfs(int u) {
low[u] = num[u] = ++dfn;
st.push(u);
for (int i = 0;i < edge[u].size();i++) {
int v = edge[u][i];
if (num[v] == 0) {
dfs(v);
low[u] = min(low[v], low[u]);
}
else if (scc[v] == 0)low[u] = min(low[u], num[v]);
}
if (low[u] == num[u]) {
cnt++;
int now;
do {
now = st.top();
st.pop();
scc[now] = cnt;
ans[cnt] += w[now];
} while (now != u);
}
}
vector<int>e[MAXN];
int res[MAXN];
int topo() {
queue<int>q;
for (int i = 1;i <= cnt;i++) {
if (in[i] == 0) {
q.push(i); res[i] = ans[i]; }
}
int maxn = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = 0;i < e[now].size();i++) {
int v = e[now][i];
in[v]--;
res[v] = max(res[v], res[now] + ans[v]);
if (in[v] == 0) {
q.push(v);
}
}
}
for (int i = 1;i <= cnt;i++) {
if (maxn < res[i])maxn = res[i];
}
return maxn;
}
int main() {
cin >> n >> m;
for (int i = 1;i <= n;i++)cin >> w[i];
for (int i = 1;i <= m;i++) {
int from, to;
cin >> from >> to;
edge[from].push_back(to);
}
for (int i = 1;i <= n;i++) {
if (!num[i])dfs(i);
}
for (int i = 1;i <=n;i++){
for (int j = 0;j < edge[i].size();j++) {
int p = edge[i][j];
if (scc[i] == scc[p])continue;
in[scc[p]]++;
e[scc[i]].push_back(scc[p]);
}
}
cout << topo() << endl;
}
P2341 【USACO03FALL】【HAOI2006】受欢迎的牛 G
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1e4+5;
const int INF = 2e9 + 7;
const int mod = 100003;
int n, m;
int w[MAXN];
int num[MAXN], low[MAXN];
int scc[MAXN],ans[MAXN];
int in[MAXN],out[MAXN];
int dfn =0,cnt= 0;
stack<int>st;
vector<int>edge[MAXN];
vector<int>e[MAXN];
int pp;
void dfs(int u) {
num[u] = low[u] = ++dfn;
st.push(u);
for (int i = 0;i < edge[u].size();i++) {
int v = edge[u][i];
if (!num[v]) {
dfs(v);
low[u] = min(low[v], low[u]);
}
else if (scc[i] == 0) {
low[u] = min(low[u], num[v]);
}
}
if (low[u] == num[u]) {
int now;
cnt++;
do {
now = st.top();
st.pop();
scc[now] = cnt;
ans[cnt]++;
} while (now != u);
}
}
void solve() {
int ff = 0;
for (int i = 1;i <= cnt;i++) {
if (out[i] == 0) {
if (ff) {
cout << 0 << endl;
return;
}
ff = i;
}
}
cout << ans[ff] << endl;
}
int main() {
cin >> n >> m;
for (int i = 1;i <= m;i++) {
int from, to;
cin >> from >> to;
edge[from].push_back(to);
}
for (int i = 1;i <= n;i++) {
if (!num[i]) {
dfs(i); }
}
for (int i = 1;i <= n;i++) {
for (int j = 0;j < edge[i].size();j++) {
int v = edge[i][j];
if (scc[i] == scc[v])continue;
out[scc[i]]++;
}
}
solve();
}
P2746 [USACO5.3]校园网Network of Schools
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1e4+5;
const int INF = 2e9 + 7;
const int mod = 100003;
int n;
int w[MAXN];
int num[MAXN], low[MAXN];
int scc[MAXN],ans[MAXN];
int in[MAXN],out[MAXN];
int dfn =0,cnt= 0;
stack<int>st;
vector<int>edge[MAXN];
vector<int>e[MAXN];
int pp;
void dfs(int u) {
low[u] = num[u] = ++dfn;
st.push(u);
for (int i = 0;i < edge[u].size();i++) {
int v = edge[u][i];
if (num[v] == 0) {
dfs(v);
low[u] = min(low[v], low[u]);
}
else if (scc[v] == 0) {
low[u] = min(low[u], num[v]);
}
}
if (low[u] == num[u]) {
int now;
cnt++;
do {
now = st.top();
st.pop();
scc[now] = cnt;
} while (now != u);
}
}
int res1, res2;
void solve() {
for (int i = 1;i <= cnt;i++) {
if (in[i] == 0)res1++;
if (out[i] == 0)res2++;
}
res2 = max(res2, res1);
if (cnt == 1)res2 = 0;
cout << res1 << endl;
cout << res2 << endl;
}
int main() {
cin >> n;
for (int i = 1;i <= n;i++) {
int to;
cin >> to;
while (to != 0) {
edge[i].push_back(to);
cin >> to;
}
}
for (int i = 1;i <= n;i++) {
if (!num[i])dfs(i);
}
for (int i = 1;i <= n;i++) {
for (int j = 0;j < edge[i].size();j++) {
int v = edge[i][j];
if (scc[v] == scc[i])continue;
in[scc[v]]++, out[scc[i]]++;
}
}
solve();
}
P2047 [NOI2007]社交网络
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 105;
int mp[MAXN][MAXN];
ll num[MAXN][MAXN];
double ans[MAXN] = {
0 };
int n, m;
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1;i < MAXN;i++) {
for (int j = 1;j < MAXN;j++) {
mp[i][j] = 999999999;
}
}
for (int i = 1;i <= m;i++) {
int a, b, w;
cin >> a >> b >> w;
mp[a][b] = w;
mp[b][a] = w;
num[a][b] = num[b][a] = 1;
}
for (int k = 1;k <= n;k++) {
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= n;j++) {
if (mp[i][j] > mp[i][k] + mp[k][j]) {
mp[i][j] = mp[i][k] + mp[k][j];
num[i][j] = num[i][k] * num[k][j];
}
else if (mp[i][j] == mp[i][k] + mp[k][j]) {
num[i][j]+= num[i][k] * num[k][j];
}
}
}
}
for (int k = 1;k <= n;k++) {
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= n;j++) {
if (k == i || k == j || i == j)continue;
if (mp[i][k] + mp[k][j] == mp[i][j]) {
ans[k] += 1.0 * num[i][k] * num[k][j] / num[i][j];
}
}
}
}
for (int i = 1;i <= n;i++) {
cout << fixed << setprecision(3) << ans[i] << endl;
}
return 0;
}
P2966 [USACO09DEC]Cow Toll Paths G
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 255;
int mp[MAXN][MAXN];
int dis[MAXN][MAXN];
int val[MAXN];
int md[MAXN][MAXN];
int ran[MAXN];
int n, m,p;
struct Node {
int id, val;
bool operator<(const Node& a) const{
return val < a.val;
}
};
Node node[MAXN];
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> m >> p;
for (int i = 1;i <= n;i++) {
cin >> node[i].val; node[i].id = i; }
sort(node + 1, node + n + 1);
for (int i = 1;i <= n;i++) {
ran[node[i].id] = i;
}
memset(mp, 0x3f, sizeof mp);
for (int i = 1;i <= n;i++)mp[i][i] = 0;
for (int i = 1;i <= m;i++) {
int a, b, w;
cin >> a >> b >> w;
mp[ran[a]][ran[b]] = mp[ran[b]][ran[a]] = min(mp[ran[a]][ran[b]],w);
}
memset(dis, 0x3f, sizeof dis);
for (int k = 1;k <= n;k++) {
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= n;j++) {
if (i == j)continue;
if (mp[i][j] > mp[i][k] + mp[k][j]) {
mp[i][j] = mp[i][k] + mp[k][j];
}
int maxn = max(node[k].val, max(node[i].val, node[j].val));
dis[i][j] = min(dis[i][j], mp[i][j] + maxn);
}
}
}
for (int i = 1;i <= p;i++) {
int s,t;
cin >> s >> t;
cout << dis[ran[s]][ran[t]] << endl;
}
return 0;
}
P3379 【模板】最近公共祖先(LCA)
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN = 5e5 + 5;
int n, m,s;
vector<int>edge[MAXN];
int dep[MAXN];
int nex[MAXN][21];
void dfs( int x,int fa) {
dep[x] =dep[fa]+1 ;
nex[x][0] = fa;
for (int i = 1;(1 << i) <= dep[x];i++) {
nex[x][i] = nex[nex[x][i - 1]][i - 1];
}
for (int i = 0;i < edge[x].size();i++) {
int v = edge[x][i];
if (v!=fa) {
dfs(v,x);
}
}
}
int lca(int a, int b) {
if (dep[a] < dep[b])swap(a, b);
for (int i = 20;i >= 0;i--) {
if (dep[a] - (1 << i) >= dep[b])a = nex[a][i];
}
if (a == b)return a;
for (int i = 20;i >= 0;i--) {
if (nex[a][i] != nex[b][i]) {
a = nex[a][i], b = nex[b][i];
}
}
return nex[a][0];
}
int main(){
std::ios::sync_with_stdio(false);
cin >> n >> m >> s;
for (int i = 1;i <= n-1;i++) {
int a, b;
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
dfs( s,0);
for (int i = 1;i <= m;i++) {
int a, b;
cin >> a >> b;
cout << lca(a, b) << endl;
}
}
P1967 货车运输
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1e4 + 5;
int n, m, t;
int fa[MAXN];
int f[MAXN][21];
int d[MAXN];
int w[MAXN][21];
bool vis[MAXN];
int findf(int x) {
if (x != fa[x]) fa[x] = findf(fa[x]);
return fa[x];
}
struct Edge {
int from, to, w;
bool operator<(const Edge& a)const {
return w > a.w;
}
};
const int MAXM = 5e4 + 5;
Edge e[MAXM];
vector<Edge>edge[MAXN];
void dfs(int x) {
vis[x] = true;
for (int i = 0;i < edge[x].size();i++) {
int v = edge[x][i].to;
if (vis[v])continue;
w[v][0] =edge[x][i].w;
d[v] = d[x] + 1;
f[v][0] = x;
dfs(v);
}
}
int lca(int a, int b) {
int ans = 999999999;
if (d[a] < d[b])swap(a, b);
for (int i = 20;i >= 0;i--) {
if (d[a] - (1 << i) >= d[b]) {
ans = min(w[a][i], ans);
a = f[a][i];
}
}
if (a == b)return ans;
for (int i = 20;i >= 0;i--) {
if (f[a][i] != f[b][i]) {
ans = min(ans, min(w[a][i], w[b][i]));
a = f[a][i];
b = f[b][i];
}
}
ans = min(ans, min(w[a][0], w[b][0]));
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1;i <= m;i++) {
cin >> e[i].from >> e[i].to >> e[i].w;
}
sort(e + 1, e + 1 + m);
for (int i = 1;i <= n;i++) {
fa[i] = i;}
for (int i = 1;i <= m;i++) {
int f1 = findf(e[i].from);
int f2 = findf(e[i].to);
if (f1 == f2)continue;
else {
fa[f1] = f2;
edge[e[i].from].push_back({
e[i].from,e[i].to,e[i].w });
edge[e[i].to].push_back({
e[i].to,e[i].from,e[i].w });
}
}
for (int i = 1;i <= n;i++) {
if (!vis[i]) {
d[i] = 1;
dfs(i);
w[i][0] = 999999999;
f[i][0] = i;
}
}
for (int i = 1;i <= 20;i++) {
for (int j = 1;j <= n;j++) {
f[j][i] = f[f[j][i - 1]][i - 1];
w[j][i] = min(w[j][i - 1], w[f[j][i - 1]][i - 1]);
}
}
int q;
cin >> q;
for (int i = 1;i <= q;i++) {
int from, to;
cin >> from >> to;
if (findf(from)!= findf(to)) {
cout << -1 << endl;
continue;
}
else cout << lca(from, to) << endl;
}
}
P1991 无线通讯网
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1e3 + 5;
int n, m, t;
int fa[MAXN];
int f[MAXN][21];
int d[MAXN];
int w[MAXN][21];
bool vis[MAXN];
int findf(int x) {
if (x != fa[x]) fa[x] = findf(fa[x]);
return fa[x];
}
struct Node {
int x,y,id;
}node[MAXN];
struct Edge {
Node a, b;
double dis;
bool operator<(const Edge& x) {
return dis < x.dis;
}
};
vector<Edge>e;
vector<Edge>edge;
double getdis(Node a, Node b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main() {
cin >> n >> m;
for (int i = 1;i <= m;i++) {
cin >> node[i].x >> node[i].y;
node[i].id = i;
}
for (int i = 1;i <= m;i++) {
for (int j = 1;j <= m;j++) {
if (i == j)continue;
e.push_back({
node[i],node[j],getdis(node[i],node[j]) });
}
}
sort(e.begin(), e.end());
int sz = e.size();
for (int i = 1;i <= m;i++)fa[i] = i;
for (int i = 0;i < sz;i++) {
Edge now = e[i];
int a = now.a.id;
int b = now.b.id;
double dis = now.dis;
int z1 = findf(a), z2 = findf(b);
if (z1 == z2)continue;
fa[z1] = z2;
edge.push_back({
node[a],node[b],dis });
}
sort(edge.begin(), edge.end());
int len = edge.size();
if (n == 1) {
cout << fixed << setprecision(2)<< edge[len - 1].dis << endl;
}
else {
double minn = edge[len - 1-(n-1)].dis;
cout <<fixed<<setprecision(2)<< minn << endl;
}
}
P4047 [JSOI2010]部落划分
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 1e3 + 5;
int n, m, t;
int fa[MAXN];
int f[MAXN][21];
int d[MAXN];
int w[MAXN][21];
bool vis[MAXN];
int findf(int x) {
if (x != fa[x]) fa[x] = findf(fa[x]);
return fa[x];
}
struct Node {
int x,y,id;
}node[MAXN];
struct Edge {
Node a, b;
double dis;
bool operator<(const Edge& x) {
return dis < x.dis;
}
};
vector<Edge>e;
vector<Edge>edge;
double getdis(Node a, Node b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main() {
cin >> n >> m;
for (int i = 1;i <= n;i++) {
cin >> node[i].x >> node[i].y;
node[i].id = i;
}
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= n;j++) {
if (i == j)continue;
e.push_back({
node[i],node[j],getdis(node[i],node[j]) });
}
}
sort(e.begin(), e.end());
int sz = e.size();
for (int i = 1;i <= n;i++)fa[i] = i;
for (int i = 0;i < sz;i++) {
Edge now = e[i];
int a = now.a.id;
int b = now.b.id;
double dis = now.dis;
int z1 = findf(a), z2 = findf(b);
if (z1 == z2)continue;
fa[z1] = z2;
edge.push_back({
node[a],node[b],dis });
}
sort(edge.begin(), edge.end());
int len = edge.size();
double minn = edge[len - 1-(m-2)].dis;
cout <<fixed<<setprecision(2)<< minn << endl;
}
P2446 [SDOI2010]大陆争霸
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN =3e3 + 5;
const int INF = 0x3f3f3f3f;
int n, m, t;
struct Edge {
int from, to, w;
};
vector<Edge>edge[MAXN];
struct Node {
int id, dis_n;
bool operator<(const Node& a)const {
return dis_n > a.dis_n;
}
};
int dis[MAXN];
int arr[MAXN];
int into[MAXN];
bool isdone[MAXN];
vector<Edge>e[MAXN];
int in[MAXN];
bool vis[MAXN];
void dj() {
memset(dis, 0x3f, sizeof dis);
memset(arr, 0x3f, sizeof arr);
priority_queue<Node>q;
dis[1] = arr[1]=into[1]=0;
q.push({
1,0 });
while (!q.empty()) {
Node now = q.top();
q.pop();
int u = now.id;
if (isdone[u])continue;
isdone[u] = true;
for (int i = 0;i < edge[u].size();i++) {
int v = edge[u][i].to;
int w = edge[u][i].w;
if (isdone[v])continue;
if (arr[v] > dis[u] + w) {
arr[v] = dis[u] + w;
if (in[v] == 0) {
dis[v] = max(into[v], arr[v]);
q.push({
v,dis[v]});
}
}
}
for (int i = 0;i < e[u].size();i++) {
int v = e[u][i].to;
into[v] = max(into[v], dis[u]);
in[v]--;
if (in[v] == 0) {
dis[v] = max(arr[v], into[v]);
q.push({
v,dis[v] });
}
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1;i <= m;i++) {
int from, to, w;
cin >> from >> to >> w;
edge[from].push_back({
from,to,w });
}
for (int i = 1;i <= n;i++) {
int c;
cin >> c;
if (c == 0)continue;
for (int j = 1;j <= c;j++) {
int from;
cin >> from;
e[from].push_back({
from,i,INF});
in[i]++;
}
}
dj();
cout << dis[n] << endl;
}