标签:多少 msu sim spl 型号 long printf 记录 过程
先跑一次最短路,再将每一个点的方案数算出来,根据乘法远离相乘
1 #include <bits/stdc++.h> 2 using namespace std; 3 const long long mod = (1 << 31) - 1; 4 const int maxn = 1010; 5 const int maxm = 1e6; 6 int n, m, head[maxn], size, mp[maxn][maxn], dis[maxn]; 7 bool bj[maxn]; 8 long long ans = 1; 9 struct edge { 10 int v, nex, w; 11 } e[maxm << 1]; 12 13 struct node { 14 int pos, cost; 15 node(int pos = 0, int cost = 0) : pos(pos), cost(cost) {} 16 friend bool operator<(node a, node b) { return a.cost > b.cost; } 17 }; 18 19 void adde(int u, int v, int w) { 20 e[size].v = v; 21 e[size].w = w; 22 e[size].nex = head[u]; 23 head[u] = size++; 24 } 25 26 void spfa() { 27 dis[1] = 0, bj[1] = 0; 28 priority_queue<node> q; 29 q.push(node(1, 0)); 30 while (!q.empty()) { 31 node a = q.top(); 32 q.pop(); 33 bj[a.pos] = 0; 34 int u = a.pos; 35 for (int i = head[u]; ~i; i = e[i].nex) { 36 int v = e[i].v, w = e[i].w; 37 if (dis[v] > dis[u] + w) { 38 dis[v] = dis[u] + w; 39 if (!bj[v]) { 40 bj[v] = 1; 41 q.push(node(v, dis[v])); 42 } 43 } 44 } 45 } 46 } 47 48 int main() { 49 // freopen("in.txt","r",stdin); 50 memset(head, -1, sizeof(head)); 51 memset(mp, 0x3f, sizeof(mp)); 52 memset(dis, 0x3f, sizeof(dis)); 53 scanf("%d%d", &n, &m); 54 for (int i = 1; i <= m; i++) { 55 int u, v, w; 56 scanf("%d%d%d", &u, &v, &w); 57 mp[u][v] = mp[v][u] = min(mp[u][v], w); 58 adde(u, v, w); 59 adde(v, u, w); 60 } 61 spfa(); 62 for (int i = 2; i <= n; i++) { 63 long long cnt = 0; 64 for (int j = 1; j <= n; j++) { 65 if (mp[j][i] < 300 && dis[j] + mp[j][i] == dis[i]) 66 cnt++; 67 } 68 ans = (ans * cnt) % mod; 69 // printf("%d\n",ans); 70 } 71 printf("%lld", ans); 72 return 0; 73 }
即求最小生成树第k长的边
证明过程可参考 https://www.cnblogs.com/mxrmxr/p/9846958.html
先将dis数组的值初始化成与第一个借点的距离,即默认第一个点已在生成树上,在进行加边操作
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=505; 4 int dis[maxn],n,k,x[maxn],y[maxn]; 5 bool vis[maxn]; 6 int cc(int a,int b){ 7 return (x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]); 8 } 9 bool cmp(int a,int b){ 10 return a>b; 11 } 12 int main(){ 13 scanf("%d%d",&n,&k); 14 for(int i=1;i<=n;i++){ 15 scanf("%d%d",x+i,y+i); 16 dis[i]=cc(1,i); 17 } 18 for(int i=1;i<n;i++){ 19 int minn=1e9,pos; 20 for(int j=2;j<=n;j++) if(!vis[j]&&dis[j]<minn) minn=dis[pos=j]; 21 vis[pos]=1; 22 for(int j=2;j<=n;j++) if(!vis[j]) dis[j]=min(dis[j],cc(j,pos)); 23 } 24 sort(dis+1,dis+1+n,cmp); 25 printf("%.2f",sqrt(dis[k])); 26 return 0; 27 }
标签:多少 msu sim spl 型号 long printf 记录 过程
原文地址:https://www.cnblogs.com/plysc/p/10408311.html