POJ 2485-Highways

POJ 2485-Highways Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22907 Accepted: 10553 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They’re planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system. Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town. Input The first line of input is an integer T, which tells how many test cases followed. The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case. Output For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum. Sample Input 1 3 0 990 692 990 0 179 692 179 0 Sample Output 692 Hint Huge input,scanf is recommended. Source POJ Contest,Author:Mathematica@ZSU


原始的最小生成树算法,但是要注意题目要我们求解的是什么:
which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.
最小生成树中的最长路径,而不是最小生成树!读题一定要仔细。 代码如下: [cpp] #include<stdio.h> int a[501][501]; int lowcost[501]; int closet[501]; //prim算法求最小生成树 int prim(int n) { for(int i=0;i<n;i++) { lowcost[i]=a[0][i]; closet[i]=0;//这个数组可以不要 } int total_min_len=0; int k,curr_min_len; for(int t=0;t<n-1;t++) { k=0; curr_min_len=65537; for(int i=0;i<n;i++) { if(lowcost[i]!=0&&lowcost[i]<curr_min_len) { curr_min_len=lowcost[i]; k=i; } } //total_min_len+=curr_min_len; if(curr_min_len>total_min_len)//注意看题,求最小生成树中的最长路径 total_min_len=curr_min_len; lowcost[k]=0; for(int i=0;i<n;i++) { if(a[i][k]!=0&&a[i][k]<lowcost[i]) { lowcost[i]=a[i][k]; closet[i]=k; } } } return total_min_len; } int main() { int t,n; scanf("%d",&t); while(t–) { scanf("%d",&n); for(int i=0;i<n;i++) for(int j=0;j<n;j++) scanf("%d",&a[i][j]); printf("%d\n",prim(n)); } return 0; } [/cpp] 本代码提交AC,用时157MS,内存528K。]]>

1 thought on “POJ 2485-Highways

  1. Pingback: POJ 1258-Agri-Net | bitJoy > code

Leave a Reply

Your email address will not be published. Required fields are marked *