欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

上海理工大学第二届“联想杯”全国程序设计邀请赛 - Dahno Dahno(SW)

发布时间:2024/4/11 编程问答 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 上海理工大学第二届“联想杯”全国程序设计邀请赛 - Dahno Dahno(SW) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目链接:点击查看

题目大意:给出一个有 nnn 个点的无向图,要求将其分成两个集合,使得总权值最大,每个集合需要非空

题目分析:SW模板题,套上即可

代码:

// Problem: Dahno Dahno // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/17574/D // Memory Limit: 524288 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) x&-x using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=510; LL maze[N][N]; LL v[N],wg[N],vis[N]; LL SW(int n) {LL res = -1;for(int i = 0; i < n; ++i) v[i] = i; //点标号的马甲,一开始都是自己。while(n > 1){memset(vis,0,sizeof(vis)); //标记数组memset(wg,0,sizeof(wg)); //权和数组int pre = 0; //起点为0号点vis[pre] = 1; //标记起点,虽无实际用处,仅为提醒自己for(int i = 1; i < n; ++i){int p = -1;for(int j = 1; j < n; ++j) if(!vis[v[j]]){ //寻找下个拓展点wg[v[j]] += maze[v[pre]][v[j]]; //下个点的权和加上边权if(p == -1 || wg[v[j]] > wg[v[p]]) p = j; //寻找wg值最大的点}vis[v[p]] = 1; //标记下个点已经遍历if(i == n - 1){ //最后个点,需要合并if(res == -1) res = wg[v[p]];else res = min(res,wg[v[p]]); //更新res,取最小割for(int j = 0; j < n; ++j){maze[v[pre]][v[j]] += maze[v[p]][v[j]];maze[v[j]][v[pre]] += maze[v[j]][v[p]];}v[p] = v[--n];}pre = p;}}return res; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n;read(n);LL sum=0;for(int i=0;i<n;i++) {for(int j=0;j<n;j++) {read(maze[i][j]);sum+=maze[i][j];}}printf("%lld\n",sum-2*SW(n));return 0; }

总结

以上是生活随笔为你收集整理的上海理工大学第二届“联想杯”全国程序设计邀请赛 - Dahno Dahno(SW)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。