最小生成树( Highways + Agri-Net)

来源:互联网 发布:奥地利 知乎 编辑:程序博客网 时间:2024/06/05 07:20

Highways

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 115   Accepted Submission(s) : 32
Problem 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. <br>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
130 990 692990 0 179692 179 0
 

Sample Output

692

Agri-Net

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 120   Accepted Submission(s) : 28
Problem Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. The distance between any two farms will not exceed 100,000. 
 

Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
 

Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
 

Sample Input
40 4 9 214 0 8 179 8 0 1621 17 16 0
 

Sample Output
28

1题意:最小生成树问题

给出n*n的无向图,即每条边的权值,求最小生成树的一个权值最大的边

2题意:输入都是一样的意思

求最小生成树

思路:1题就是在所给出的图上求一次最小生成树 取最长边即可

A>C 692 C>B 179

2题更直接了

A>B 4 B>C 8 C>D 16

1.

#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int a[1001][1001];void solve(int n){    int i,k,ans;    int val[1001];    memset(val,0x3f3f,sizeof(val));    bool vis[1001];    vis[0]=1;    for(i=1;i<n;i++)    {        vis[i]=0;        val[i]=a[0][i];    }    memset(vis,0,sizeof(vis));    ans=0;    for(k=1;k<n;k++)    {        int j,min=0x3f3f;        for(i=1;i<n;i++)            if(min>val[i]&&!vis[i]){min=val[i],j=i;}        if(ans<min)ans=min;        vis[j]=1;        for(i=1;i<n;i++)            if(val[i]>a[j][i]&&!vis[i])val[i]=a[j][i];    }    cout<<ans<<endl;}int main(){    int i,j;    int t;    int n;    cin>>t;    while(t--)    {        memset(a,0x3f3f,sizeof(a));        cin>>n;        for(i=0;i<n;i++)            for(j=0;j<n;j++)                scanf("%d",&a[i][j]);        solve(n);    }    return 0;}
2.
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int a[1001][1001];void solve(int n){    int i,j,k,ans;    int val[1001];    memset(val,0x3f3f3f,sizeof(val));    bool vis[1001];    for(i=0;i<=n;i++)    {        vis[i]=0;        val[i]=a[1][i];    }    for(i=1;i<=n;i++)    {        int v,min=0x3f3f3f;        for(j=1;j<=n;j++)            if(!vis[j]&&val[j]<min)            {                min=val[j];                v=j;                }        vis[v]=1;        for(j=1;j<=n;j++)            if(!vis[j]&&val[j]>a[v][j])                val[j]=a[v][j];    }    ans=0;    for(i=1;i<=n;i++)    {        //cout<<val[i]<<endl;        ans+=val[i];    }    cout<<ans<<endl;}int main(){    int i,j;    int t;    int n;    while(cin>>n)    {        memset(a,0,sizeof(a));        for(i=1;i<=n;i++)            for(j=1;j<=n;j++)                scanf("%d",&a[i][j]);        solve(n);    }    return 0;}