链接:戳这里
思路:莫名其妙教练报的蓝桥杯 估计会GG
代码:
#include<bits/stdc++.h> using namespace std; int n,m; int a[110][110]; struct node{ int x,y; char c; node(){} node(int x,int y,char c):x(x),y(y),c(c){} }; queue<node> qu; int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ scanf("%d",&a[i][j]); } } int x,y,k; char c[2]; scanf("%d%d%s%d",&x,&y,&c,&k); node tmp; tmp.x=x;tmp.y=y;tmp.c=c[0]; qu.push(tmp); while(!qu.empty()){ node next,now=qu.front(); qu.pop(); ///printf("k=%d %d %d %c\n",k,now.x,now.y,now.c); if(k) k--; else { printf("%d %d\n",now.x,now.y); return 0; } if(now.c=='L'){ if(a[now.x][now.y]==0){ next.x=now.x+1; next.y=now.y; next.c='D'; a[now.x][now.y]=1; qu.push(next); } else{ next.x=now.x-1; next.y=now.y; next.c='U'; a[now.x][now.y]=0; qu.push(next); } } else if(now.c=='R'){ if(a[now.x][now.y]==0){ next.x=now.x-1; next.y=now.y; next.c='U'; a[now.x][now.y]=1; qu.push(next); } else { next.x=now.x+1; next.y=now.y; next.c='D'; a[now.x][now.y]=0; qu.push(next); } } else if(now.c=='U'){ if(a[now.x][now.y]==0){ next.x=now.x; next.y=now.y-1; next.c='L'; a[now.x][now.y]=1; qu.push(next); } else { next.x=now.x; next.y=now.y+1; next.c='R'; a[now.x][now.y]=0; qu.push(next); } } else if(now.c=='D'){ if(a[now.x][now.y]==0){ next.x=now.x; next.y=now.y+1; next.c='R'; a[now.x][now.y]=1; qu.push(next); } else { next.x=now.x; next.y=now.y-1; next.c='L'; a[now.x][now.y]=0; qu.push(next); } } } return 0; }
链接:戳这里
首先处理不需要建设码头的最小生成树 得到值ans。
然后 加一个0点 连接可以建设的每一个码头 加入邻接表 跑最小生成树 去min
代码:
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #define INF 1e20 using namespace std; int n,m,tot; struct edge{ int u,v,w,next,f; } e[500100]; bool cmp(edge a,edge b){ return a.w<b.w; } int fa[10010],head[10010],a[10010]; int find(int x){ if(x!=fa[x]){ fa[x]=find(fa[x]); } return fa[x]; } void init(){ for(int i=0;i<=n;i++) fa[i]=i; for(int i=1;i<=n;i++) head[i]=-1; tot=0; } void add(int U,int V,int W,int f){ e[tot].u=U; e[tot].v=V; e[tot].w=W; e[tot].next=head[U]; e[tot].f=f; head[U]=tot++; } int main(){ scanf("%d%d",&n,&m); init(); long long ans=0,sum=0,anw=INF; for(int i=1;i<=m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); if(w<0){ add(u,v,0,0); add(v,u,0,0); ans+=(long long)-w; } else { add(u,v,w,0); add(v,u,w,0); } } sort(e,e+tot,cmp); for(int i=0;i<tot;i++){ int xx=find(e[i].u); int yy=find(e[i].v); if(xx!=yy){ fa[xx]=yy; sum+=(long long)e[i].w; } } int flag=0; for(int i=2;i<=n;i++){ if(find(1)!=find(i)){ flag=1; break; } } if(!flag) anw=min(anw,sum); ///printf("%I64d\n",anw); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]!=-1) { add(0,i,a[i],1); add(i,0,a[i],1); } } sort(e,e+tot,cmp); for(int i=0;i<=n;i++) fa[i]=i; sum=0; for(int i=0;i<tot;i++){ int xx=find(e[i].u); int yy=find(e[i].v); if(xx!=yy){ fa[xx]=yy; sum+=(long long)e[i].w; } } ///printf("%I64d %I64d\n",anw,sum); anw=min(anw,sum); printf("%I64d\n",anw-ans); return 0; } /* 5 5 1 3 10 1 2 10 2 3 10 2 4 10 4 5 10 1 1 1 1 1 5 10 5 2 8 4 5 8 2 5 10 1 4 4 5 3 5 1 2 9 1 5 8 5 1 2 1 3 5 4 3 12 -1 -1 -1 18 11 19 */