未完成存档
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了未完成存档相关的知识,希望对你有一定的参考价值。
//http://www.cogs.pro/cogs/problem/problem.php?pid=290 #include <cstring> #include <string> #include <cstdio> #include <queue> #include <cmath> #include <map> #define inf 0x7fffffff #define M 100050 #define N 200 using namespace std; typedef long long LL; map<pair<int,int>,int>cz; map<string,int>q; struct Edge { int next,to,flow,cost; Edge (int next=0,int to=0,int flow=0,int cost=0) : next(next),to(to),flow(flow),cost(cost) {} }edge[M]; char name[40]; bool vis[N]; int fa[N],dis[N],flow[N],head[N*2],cnt,k,n,Map[N][N],x[N],y[N]; double calc(LL x1,LL y1,LL x2,LL y2) {return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));} bool judge(int x1,int y1,int x2,int y2) { if(x1!=x2&&y1!=y2) { int k=(y2-y1)/(x2-x1); for(;x1<x2&&y1<y2;) { x1+=k; y1+=k; if(cz[make_pair(x1,y1)]) return false; } } else if(x1==x2) { y1=min(y1,y2);y2=max(y1,y2); for(;y1+1<y2;) { y1++; if(cz[make_pair(x1,y1)]) return false; } } else if(y1==y2) { x1=min(x1,x2);x2=max(x1,x2); for(;x1+1<x2;) { x1++; if(cz[make_pair(x1,y1)]) return false; } } return true; } inline int min(int a,int b) {return a>b?b:a;} inline void ins(int u,int v,int w,int f) { edge[++cnt]=Edge(head[u],v,w,f); head[u]=cnt; } bool spfa(int s,int t) { for(int i=s;i<=t;++i) dis[i]=flow[i]=inf,vis[i]=0; dis[s]=0; fa[s]=0; queue<int>q; q.push(s); for(int now;!q.empty();) { now=q.front(); q.pop(); vis[now]=0; for(int i=head[now];i;i=edge[i].next) { int v=edge[i].to; if(dis[v]>dis[now]+edge[i].cost&&edge[i].flow) { dis[v]=dis[now]+edge[i].cost; flow[v]=min(flow[now],edge[i].flow); fa[v]=i; if(!vis[v]) { q.push(v); vis[v]=1; } } } } return dis[t]<inf; } int update(int s,int t) { int x=flow[t]; for(int i=t;i!=s&&i;i=edge[fa[i]^1].to) { edge[fa[i]].flow-=x; edge[fa[i]^1].flow+=x; } return dis[t]*x; } int Dinic(int s,int t) { int ans=0; for(;spfa(s,t);ans+=update(s,t)); return ans; } int main() { scanf("%d%d",&k,&n); for(int i=1;i<=n*2;++i) { scanf("%d%d",&x[i],&y[i]); scanf("%s",name); int len=strlen(name); for(int j=0;j<len;++j) if(name[j]>=‘A‘&&name[j]<=‘Z‘) name[j]+=32; q[name]=i; cz[make_pair(x[i],y[i])]=1; } for(int i=1;i<=n;++i) for(int j=n+1;j<=n*2;++j) Map[i][j]=1; char name1[50],name2[50]; int yf; for(;;) { scanf("%s",name1); if(name1[0]==‘E‘&&name1[1]==‘n‘&&name1[2]==‘d‘) break; int len=strlen(name1); for(int i=0;i<len;++i) if(name1[i]>=‘A‘&&name1[i]<=‘Z‘) name1[i]+=32; scanf("%s",name2); len=strlen(name2); for(int i=0;i<len;++i) if(name2[i]>=‘A‘&&name2[i]<=‘Z‘) name2[i]+=32; int a=q[name1],b=q[name2]; if(a>b) swap(a,b); scanf("%d",&yf); if(calc((LL)x[a],(LL)y[a],(LL)x[b],(LL)y[b])<=(double)k&&judge(x[a],y[a],x[b],y[b])) Map[a][b]=yf; } int s=0,t=n*2+1; for(int i=1;i<=n;++i) ins(s,i,1,0),ins(i,s,0,0); for(int i=n+1;i<=n*2;++i) ins(i,t,1,0),ins(t,i,0,0); for(int i=1;i<=n;++i) for(int j=n+1;j<=n*2;++j) ins(i,j,1,Map[i][j]),ins(j,i,0,-Map[i][j]); printf("%d\n",Dinic(s,t)); return 0; }
以上是关于未完成存档的主要内容,如果未能解决你的问题,请参考以下文章