uva 10803 Thunder Mountain

來源:互聯網
上載者:User

原題:
Markus is building an army to fight the evil Valhalla Sector, so he needs to move some supplies between several of the nearby towns. The woods are full of robbers and other unfriendly folk, so it’s dangerous to travel far. As Thunder Mountain’s head of security, Lee thinks that it is unsafe to carry supplies for more than 10km without visiting a town. Markus wants to know how far one would need to travel to get from one town to another in the worst case.
Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containing n (the number of towns, 1 < n < 101). The next n lines will give the xy-locations of each town in km (integers in the range [0,1023]). Assume that the Earth is flat and the whole 1024 × 1024 grid is covered by a forest with roads connecting each pair of towns that are no further than 10km away from each other.
Output
For each test case, output the line ‘Case #x:’, where x is the number of the test case. On the next line, print the maximum distance one has to travel from town A to town B (for some A and B). Round the answer to 4 decimal places. Every answer will obey the formula |ans ∗ 10 4 − ⌊ans ∗ 10 4 ⌋ − 0.5| > 10 −2
If it is impossible to get from some town to some other town, print ‘Send Kurdy’ instead. Put an empty line after each test case.
Sample Input
2
5
0 0
10 0
10 10
13 10
13 14
2
0 0
10 1
Sample Output
Case #1:
25.0000
Case #2:
Send Kurdy

中文:
給你一堆節點座標,如果兩個節點之間的距離超過10那麼這兩個節點之間被看做不可到達。現在問你給定的這個圖是否是連通的,如果不連通,輸出Send Kurdy,否則算出這個圖中所有節點到其他節點的最短路徑,然後找出最短路徑中最長的。

#include<bits/stdc++.h>using namespace std;struct point{    double x,y;};point points[110];int n;double G[110][110];double dist(const point &a,const point &b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}void floyed(){    double ans=0;    int ii,jj;    for(int k=1;k<=n;k++)    {        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                G[i][j]=min(G[i][j],G[i][k]+G[k][j]);            }        }    }}int main(){    ios::sync_with_stdio(false);    int t;    cin>>t;    int k=1;    while(t--)    {        cin>>n;        for(int i=1;i<=n;i++)            cin>>points[i].x>>points[i].y;        memset(G,0,sizeof(G));        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                double dis=dist(points[i],points[j]);                if(dis<=10)                    G[i][j]=dis;                else                    G[i][j]=INT_MAX;            }        }        floyed();        double ans=-1;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)                ans=max(ans,G[i][j]);        }        if(ans>=INT_MAX)        {            cout<<"Case #"<<k++<<':'<<endl;            cout<<"Send Kurdy"<<endl;        }        else        {            cout<<"Case #"<<k++<<':'<<endl;            cout<<fixed<<setprecision(4)<<ans<<endl;        }        cout<<endl;    }    return 0;}

解答:

這題說的好隱晦,第一範例怎麼算出來的我都想了半天。實際上就是用floyed算出最短路徑後,找出這些最短路徑當中最長的即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.