标签:this include locate key try art cti org class
Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %lld & %llu |
Description
Input
Output
Sample Input
9 1 5 0 0 3 2 4 5 5 1 0 4 5 2 1 2 5 3 3 1 3 9 7 1 2
Sample Output
1 6 3 7 4 9 5 7 8 3
Source
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<vector> 8 using namespace std; 9 const int mxn=1810; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 13 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 14 return x*f; 15 } 16 struct point{ 17 int x,y; 18 }p[mxn]; 19 double dist(int a,int b){ 20 return sqrt((double)(p[a].x-p[b].x)*(p[a].x-p[b].x)+(double)(p[a].y-p[b].y)*(p[a].y-p[b].y)); 21 } 22 double mp[mxn][mxn]; 23 int ans[mxn]; 24 int n,m; 25 int cm[mxn]; 26 double dis[mxn]; 27 bool vis[mxn]; 28 void Prim(){ 29 int i,j; 30 for(i=1;i<=n;i++){ 31 dis[i]=mp[1][i]; 32 cm[i]=1;//?????????? 33 } 34 dis[1]=0;vis[1]=1; 35 for(i=1;i<=n;i++){ 36 double mini=1e9;int pos=0; 37 for(j=1;j<=n;j++){ 38 if(!vis[j] && dis[j]<mini){ 39 mini=dis[j]; 40 pos=j; 41 } 42 } 43 if(!pos)break; 44 if(mp[cm[pos]][pos])printf("%d %d\n",cm[pos],pos); 45 vis[pos]=1; 46 for(j=1;j<=n;j++){ 47 if(!vis[j] && dis[j]>mp[pos][j]){ 48 dis[j]=mp[pos][j]; 49 cm[j]=pos; 50 } 51 } 52 } 53 return; 54 } 55 int main(){ 56 int i,j; 57 n=read(); 58 for(i=1;i<=n;i++){ 59 p[i].x=read();p[i].y=read(); 60 } 61 for(i=1;i<n;i++) 62 for(j=i+1;j<=n;j++) 63 mp[i][j]=mp[j][i]=dist(i,j); 64 m=read(); 65 int u,v; 66 for(i=1;i<=m;++i){ 67 u=read();v=read(); 68 mp[u][v]=mp[v][u]=0; 69 } 70 Prim(); 71 return 0; 72 }
/*by SilverN*/ #include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<vector> using namespacestd; const int mxn=1810; int read(){ int x=0,f=1;char ch=getchar(); while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } structpoint{ int x,y; }p[mxn]; double dist(int a,int b){ return sqrt((double)(p[a].x-p[b].x)*(p[a].x-p[b].x)+(double)(p[a].y-p[b].y)*(p[a].y-p[b].y)); } double mp[mxn][mxn]; int ans[mxn]; int n,m; int cm[mxn]; double dis[mxn]; bool vis[mxn]; void Prim(){ int i,j; for(i=1;i<=n;i++){ dis[i]=mp[1][i]; cm[i]=1;//?????????? } dis[1]=0;vis[1]=1; for(i=1;i<=n;i++){ double mini=1e9;int pos=0; for(j=1;j<=n;j++){ if(!vis[j] && dis[j]<mini){ mini=dis[j]; pos=j; } } if(!pos)break; if(mp[cm[pos]][pos])printf("%d %d\n",cm[pos],pos); vis[pos]=1; for(j=1;j<=n;j++){ if(!vis[j] && dis[j]>mp[pos][j]){ dis[j]=mp[pos][j]; cm[j]=pos; } } } return; } int main(){ int i,j; n=read(); for(i=1;i<=n;i++){ p[i].x=read();p[i].y=read(); } for(i=1;i<n;i++) for(j=i+1;j<=n;j++) mp[i][j]=mp[j][i]=dist(i,j); m=read(); int u,v; for(i=1;i<=m;++i){ u=read();v=read(); mp[u][v]=mp[v][u]=0; } Prim(); return 0; }
标签:this include locate key try art cti org class
原文地址:http://www.cnblogs.com/SilverNebula/p/6024141.html