上海理工大学第二届“联想杯”全国程序设计邀请赛 - Dahno Dahno(SW)
生活随笔
收集整理的這篇文章主要介紹了
上海理工大学第二届“联想杯”全国程序设计邀请赛 - 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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ - 2914 Minimum C
- 下一篇: 上海理工大学第二届“联想杯”全国程序设计