标签:lan inter another min line seve tun notice iostream
Input
Output
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
大意就是求任意两个点之间所有路径中最大两点距离的最小值。可以看作是Floyd的变形,只需要把转移方程修改一下即可:d[i][j]=min(d[i][j],max(d[i][k],d[k][j]))其中d[i][j]表示i到j所有路程经过的最长边的最小值。
当然也可以用dijkstra来写,松弛操作改为:if(!vis[y] && d[y]>max(d[x],a[x][y]))
d[y]=max(d[x],a[x][y]);
这其实也算是某种意义上的最短路,只不过定义不同。g++提交的话记得要把.3lf改成.3f才能过。
//Floyd #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int n; struct node { double x; double y; }nod[205]; double d[205][205]; double floyd() { int i,j,k; for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { d[i][j]=min(d[i][j],max(d[i][k],d[k][j])); } } } return d[1][2]; } int main() { int cnt=0; while(scanf("%d",&n)!=EOF&&n) { cnt++; int i,j; //memset(d,0x3f,sizeof(d)); double不能用memset for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { d[i][j]=1e9; } } for(i=1;i<=n;i++) { double x,y; scanf("%lf%lf",&x,&y); nod[i].x=x; nod[i].y=y; } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { double len=sqrt((nod[i].x-nod[j].x)*(nod[i].x-nod[j].x)+(nod[i].y-nod[j].y)*(nod[i].y-nod[j].y)); d[i][j]=d[j][i]=min(d[i][j],len); cout<<d[i][j]<<endl; } } double out=floyd(); printf("Scenario #%d\n",cnt); printf("Frog Distance = %.3lf\n\n",out); } }
//Dijkstra #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <cstring> using namespace std; double a[305][305]; double d[305]; bool vis[305]; int n; struct node { double x; double y; }nod[205]; void dijkstra() { d[1]=0; int i; for(i=1;i<n;i++) { int x=0; int j; for(j=1;j<=n;j++) { if(!vis[j]&&(x==0||d[j]<d[x]))x=j; } vis[x]=1; int y; for(y=1;y<=n;y++) { } } } int main() { int cnt=0; while(scanf("%d",&n)!=EOF&&n) { cnt++; int i,j; //memset(d,0x3f,sizeof(d)); double不能用memset for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { a[i][j]=1e9; } d[i]=1e9; vis[i]=0; } for(i=1;i<=n;i++) { double x,y; scanf("%lf%lf",&x,&y); nod[i].x=x; nod[i].y=y; } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { double len=sqrt((nod[i].x-nod[j].x)*(nod[i].x-nod[j].x)+(nod[i].y-nod[j].y)*(nod[i].y-nod[j].y)); a[i][j]=a[j][i]=min(a[i][j],len); } } // for(i=1;i<=n;i++) // { // for(j=1;j<=n;j++) // { // cout<<a[i][j]<<‘ ‘; // } // cout<<endl; // } dijkstra(); printf("Scenario #%d\nFrog Distance = %.3lf\n\n",cnt,d[2]); } }
标签:lan inter another min line seve tun notice iostream
原文地址:https://www.cnblogs.com/lipoicyclic/p/12310323.html