Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么会超时。明明本机上测和OpenJ_Bailian 都过了。自己电脑测试轻松过。 #include <stdio.h> #include <queue> #include <cmath> #include <string.h> #include <iostream> #include <algorithm> #define Pair pair<int,int> #define MAXN 600+10 #define MAXM 1200000+1 using namespace std; int l,n,m,num,head[MAXN],t,dis[MAXN][MAXN],v[MAXM]; int ans[MAXN],minn=999999999,mian[MAXN][MAXN],pre[MAXN]; int map[MAXN][MAXN]; struct { int p[MAXN]; void e() { memset(p,0,sizeof(p)); } }po[MAXN]; struct Edge{ int dis,next,to,exi,from; }edge[MAXM]; void add(int from,int to,int dis) { edge[++num].next=head[from]; edge[num].to=to; edge[num].dis=dis; edge[num].from=from; head[from]=num; } void dij(int s) { memset(v,0,sizeof(v)); priority_queue<Pair,vector<Pair>,greater<Pair> > h; for(int i=1;i<=m;i++) dis[s][i]=999999999; dis[s][s]=0; h.push(Pair(dis[s][s],s)); while(h.size()>0) { int k=h.top().second;h.pop(); if(v[k]) continue; v[k]=1; for(int i=head[k];i;i=edge[i].next) if(dis[s][k]+edge[i].dis<dis[s][edge[i].to]) { dis[s][edge[i].to]=dis[s][k]+edge[i].dis; h.push(Pair(dis[s][edge[i].to],edge[i].to)); if(s==1) pre[edge[i].to]=edge[i].from; } } } int main() { // freopen("WALLS.in","r",stdin); // freopen("WALLS.out","w",stdout); scanf("%d%d%d",&m,&n,&l); int numm=0; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) map[i][j]=map[j][i]=(++numm);//,printf("(%d,%d)-->%d\n",i,j,numm); for(int i=1;i<=l;i++) scanf("%d",&ans[i]); sort(ans+1,ans+l+1); for(int i=1;i<=m;i++) { int t=0,f=0,tt[MAXN];memset(tt,0,sizeof(tt)); scanf("%d",&t);po[i].p[0]=t; for(int j=1;j<=t;j++) { scanf("%d",&f);tt[j]=f;mian[f][++mian[f][0]]=i; if(j>1) { po[i].p[j-1]=map[tt[j]][tt[j-1]]; } } po[i].p[t]=map[tt[1]][tt[t]]; } for(int i=1;i<=m;i++) { for(int j=i+1;j<=m;j++) { int flag=0; for(int a1=1;flag==0&&a1<=po[i].p[0];a1++) for(int a2=1;flag==0&&a2<=po[j].p[0];a2++) { if(po[i].p[a1]==po[j].p[a2]) {add(i,j,1);add(j,i,1);flag=1;break;} } } } for(int i=1;i<=m;i++) dij(i); for(int i=1;i<=m;i++) { int temp=0; //printf("meiju=%d\n",i); for(int j=1;j<=l;j++) { int dc=99999999; for(int d=1;d<=mian[ans[j]][0];d++) { dc=min(dc,dis[i][mian[ans[j]][d]]); //printf(" from=%d to=%d %d \n",i,mian[ans[j]][d],dis[i][mian[ans[j]][d]]); } temp+=dc; } minn=min(minn,temp); //printf("temp=%d\n",temp); }//*/ printf("%d\n",minn); //printf("%d\n",num); //for(int i=1;i<=m;i++) //{ // printf("%d ",dis[1][i]); //}printf("\n");*/ return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator