校賽網路賽部分題解,部分題解

來源:互聯網
上載者:User

校賽網路賽部分題解,部分題解

A:一對一配對,兩種解法,一種是用二分最大權匹配 km演算法可解 ,二是用最大費用最大流,兩種都是直接套演算法的模板就可以了。

下面給出km的做法

/****************************************************** * File Name:   fff.cpp * Author:      kojimai * Create Time: Thu 26 Mar 2015 08:20:59 PM CST******************************************************/#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>using namespace std;#define FFF 35#define MAX 0x3f3f3f3fint len[FFF][FFF];int sx[FFF],sy[FFF];int lx[FFF],ly[FFF];int match[FFF],n;int bfs(int i) {sx[i] = 1;for(int j = 1;j <= n;j++) {if(!sy[j] && lx[i] + ly[j] == len[i][j]) {sy[j] = 1;if(!match[j] || bfs(match[j])) {match[j] = i;return 1;}}}return 0;}int km(int n) {for(int i = 1;i <= n;i++) {lx[i] = len[i][1];for(int j = 2;j <= n;j++)lx[i] = max(lx[i],len[i][j]);}memset(ly,0,sizeof(ly));memset(match,0,sizeof(match));for(int x = 1;x <= n;x++) {while(1) {memset(sx,0,sizeof(sx));memset(sy,0,sizeof(sy));if(bfs(x)) break;int d = MAX;for(int i = 1;i <= n;i++) {if(sx[i])for(int j = 1;j <= n;j++)if(!sy[j])d = min(d,lx[i] + ly[j] - len[i][j]);}for(int i = 1;i <= n;i++)if(sx[i])lx[i] -= d;for(int j =1;j <= n;j++)if(sy[j])ly[j] += d;}}int sum = 0;for(int i = 1;i <= n;i++)sum += lx[i];for(int i = 1;i <= n;i++)sum += ly[i];return sum;}int main() {int t;freopen("out.out","w",stdout);cin >> t;while(t--) {cin >> n;for(int i = 1;i <= n;i++)for(int j = 1;j <= n;j++)scanf("%d",&len[i][j]);cout<<km(n)<<endl;}return 0;}


B:要求除起點外每個點都經過一次,資料只到10,n!的演算法都可解,所以各種姿勢隨便過。我直接調用的全排列的函數

/****************************************************** * File Name:   pdf.cpp * Author:      kojimai * Create Time: Wed 25 Mar 2015 02:33:49 AM CST******************************************************/#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>using namespace std;#define FFF 14bool match[FFF][FFF];int ans[FFF];bool judge(int n) {for(int i = 2;i <= n;i++) {if(match[ans[i-1]][ans[i]] == false)return false;}if(match[ans[1]][ans[n]] == false)return false;elsereturn true;}int main() {int t,x,y;cin >> t;//freopen("out.out","w",stdout);while(t--) {int n,m;cin >> n >> m;memset(match,false,sizeof(match));for(int i = 0;i < m;i++) {cin >> x >> y;match[x][y] = match[y][x] = true;}for(int i = 1;i <= n;i++) {ans[i] = i;}x = n;while(judge(n)==false) {next_permutation(ans+1,ans+n+1);}for(int i = 1;i <= n;i++) {printf("%d ",ans[i]);}cout<<1<<endl;}return 0;}


C:裸的快速冪,希望大家都能掌握這個東西

/****************************************************** * File Name:   mi.cpp * Author:      kojimai * Create Time: Wed 25 Mar 2015 03:54:51 AM CST******************************************************/#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>using namespace std;#define mod 1000000007long long qpow(long long x,long long y) {long long now = 1,tmp = x;while(y) {if(y % 2)now = (now * tmp) % mod;tmp = (tmp * tmp) % mod;y /= 2;}return now;}int main() {int t;cin >> t;//freopen("out.out","w",stdout);while(t--) {long long x,y;cin >> x >> y;cout<<qpow(x,y)<<endl;}return 0;}


D:一個簡單的BFS,找到離出口的最短距離就好了

/****************************************************** * File Name:   corpse.cpp * Author:      kojimai * Create Time: Wed 25 Mar 2015 05:01:25 AM CST******************************************************/#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>#include<queue>using namespace std;#define FFF 25char s[FFF][FFF];bool vis[FFF][FFF];int move[4][2] = {1,0,-1,0,0,1,0,-1};struct node {int x,y,t;};queue<node> p;bool bfs(int sx,int sy,int n,int m,int tim) {int xx,yy;memset(vis,false,sizeof(vis));vis[sx][sy] = true;while(!p.empty()) p.pop();node now,tmp;now.x = sx;now.y = sy;now.t = 0;p.push(now);while(!p.empty()) {now = p.front();p.pop();tmp.t = now.t + 1;for(int i = 0;i < 4;i++) {xx = now.x + move[i][0];yy = now.y + move[i][1];//cout<<"xx = " << xx <<"yy = "<<yy << endl;if(xx<0 || xx >= n || yy<0 || yy>=m)continue;if(s[xx][yy] == '*')continue;if(!vis[xx][yy]) {tmp.x = xx;tmp.y = yy;if(s[xx][yy] == 'O') {return true;}if(tmp.t < tim) {vis[xx][yy] = true;p.push(tmp);}}}}return false;}int main() {int t;cin >> t;freopen("d.out","w",stdout);while(t--) {int n,m,k;int sx,sy,ex,ey;cin >> n >> m >> k;for(int i = 0;i < n;i++) {scanf("%s",s[i]);for(int j = 0;j < m;j++) {if(s[i][j] == 'X') {sx = i;sy = j;}}}bool flag = bfs(sx,sy,n,m,k);if(flag)cout<<"happy end\n";elsecout<<"bad end"<<endl;}return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.