所有的畅通工程[HDU1232][HDU1874][HDU1875][HDU1879]

畅通工程

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49230 Accepted Submission(s): 26261


Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?

Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。

Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。

Sample Input
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0

Sample Output
1
0
2
998

Hint
Hint

Huge input, scanf is recommended.

#include<iostream>  
using namespace std;  
  
const int MAX=1000;  
int father[MAX];  
  
void initial(int n)    //初始化  
{  
    for(int i=1;i<=n;i++)  
        father[i]=i;  
}  
  
int find(int x)    //查找  
{  
    while(father[x]!=x)  
        x=father[x];  
  
    return x;  
}  
  
void combine(int a,int b)   //合并  
{  
    int tmpa=find(a);  
    int tmpb=find(b);  
  
    if(tmpa!=tmpb)  
        father[tmpa]=tmpb;  
}  
  
int main()  
{  
    int i,n,m,a,b,tmp;  
  
    while(cin>>n,n)  
    {  
        initial(n);  
  
        cin>>m;  
  
        for(i=1;i<=m;i++)  
        {  
            cin>>a>>b;  
            combine(a,b);  
        }  
  
        tmp=0;  
        for(i=1;i<=n;i++)   //确定连通分量个数  
        {  
            if(father[i]==i)  
                tmp++;  
        }  
  
        cout<<tmp-1<<endl;  
    }  
  
    return 0;  
}  
View Code

 

畅通工程续

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 48676 Accepted Submission(s): 18092


Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。

Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.

Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output
2
-1

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int INF=0x3f3f3f3f;
const int N=210;

int n,m,s,t;
int map[N][N],dis[N],vis[N];

void Dijkstra(int src){
    int i;
    for(i=0;i<n;i++){
        dis[i]=map[src][i];
        vis[i]=0;
    }
    dis[src]=0;
    vis[src]=1;
    int j,k,tmp;
    for(i=0;i<n;i++){
        tmp=INF;
        for(j=0;j<n;j++)
            if(!vis[j] && tmp>dis[j]){
                k=j;
                tmp=dis[j];
            }
        if(tmp==INF)
            break;
        vis[k]=1;
        for(j=0;j<n;j++)
            if(!vis[j] && dis[j]>dis[k]+map[k][j])
                dis[j]=dis[k]+map[k][j];
    }
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d",&n,&m)){
        int u,v,w;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                map[i][j]=INF;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&w);
            if(map[u][v]>w)
                map[u][v]=map[v][u]=w;
        }
        scanf("%d%d",&s,&t);
        Dijkstra(s);
        if(dis[t]==INF)
            printf("-1
");
        else
            printf("%d
",dis[t]);
    }
    return 0;
}
View Code

 

畅通工程再续

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25697 Accepted Submission(s): 8320


Problem Description
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。

Input
输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。

Output
每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.

Sample Input
2
2
10 10
20 20
3
1 1
2 2
1000 1000

Sample Output
1414.2
oh!

#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAXN 1002 
double path[102][102];
int flag[102];
double closedge[102];
double cnt;

typedef struct{
    int x, y;
}input;

input temp[102];

double calculate(int x1, int y1, int x2, int y2)
{// 两点之间的距离 
    double t = (double)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    return sqrt(t);
    
}

double CreatMST(int n)
{
    int i, j, x;
    double k;
    flag[0] = 1;
    for(i=1; i<n; ++i)
    closedge[i] = path[0][i];
    for(i=1; i<n; ++i)
    {
        k = MAXN-2, x = 0;
        for(j=0; j<n; ++j)
        if(!flag[j] && closedge[j] <= k)
            x = j, k = closedge[j];
        flag[x] = 1;
        cnt += k;
        for(j=0; j<n; ++j)
        if(!flag[j] && closedge[j] > path[x][j])
        closedge[j] = path[x][j];
    }
    return cnt;
}

int main()
{
    int i, j, k, t, x, y, n, m, T, fflag;
    double h;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        cnt = 0;
        memset(temp, 0, sizeof(input)*102);
        memset(flag, 0, sizeof(flag));
        memset(closedge, 0, sizeof(closedge));
        memset(path, 0, sizeof(double)*102*102);
        for(i=0; i<n; ++i)
        scanf("%d%d", &temp[i].x, &temp[i].y);
        
        // 计算N*(N+1)条路径的权重 
        for(i=0; i<n; ++i)
        for(j=0; j<n; ++j)
        {
            h = calculate(temp[i].x, temp[i].y, temp[j].x, temp[j].y);
            if(h < 10 || h > 1000) path[i][j] = MAXN;  // 处理掉不属于范围内的路径 
            else path[i][j] = h;
        }
        if(n == 1 || n == 0) {printf("oh!
"); continue;}  // 特殊的情况先判断 
        h = CreatMST(n)*100;
        fflag = 0;
        for(i=0; i<n; ++i) if(flag[i] == 0) {fflag = 1; break;} // 如果还有小岛未归纳进去,说明工程未完成 
        if(fflag) printf("oh!
");
        else printf("%.1lf
", h);
    }
    return 0;
}
View Code

 

继续畅通工程

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22717 Accepted Submission(s): 9708


Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。

Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2 行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示未建。

当N为0时输入结束。

Output
每个测试用例的输出占一行,输出全省畅通需要的最低成本。

Sample Input
3
1 2 1 0
1 3 2 0
2 3 4 0
3
1 2 1 0
1 3 2 0
2 3 4 1
3
1 2 1 0
1 3 2 1
2 3 4 1
0

Sample Output
3
1
0

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

struct node {
    int start ,end,expense,flag;
}data[5005];

int father[105];
void make_set(int n)
{
    for(int i=1;i<=n;i++)
        father[i]=i;
}
int find_set(int x)
{
    if(x^father[x])
        father[x]=find_set(father[x]);
    return father[x];
}
int union_set(int x,int y)
{
    x=find_set(x);
    y=find_set(y);
    if(x==y)
        return 0;
    father[x]=y;
    return 1;
}
bool cmp(node a,node b)
{
    return a.expense<b.expense;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(!n)
            break;
        make_set(n);
        int ans=0;
        int m=(n-1)*n/2;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&data[i].start,&data[i].end,&data[i].expense,&data[i].flag);
            if(data[i].flag)//当道路修通时,规定一节点为另一节点的父亲
                father[data[i].start]=data[i].end;
        }
        sort(data,data+m,cmp);//按道路的花费升序排列
        
        //在不构成环的前提下,选择最短的边,有贪心的思想
        for(int i=0;i<m;i++)
        {
            if(union_set(data[i].start,data[i].end))
                ans+=data[i].expense;
        }
        printf("%d
",ans);
    }
    return 0;
}
View Code

 

原文地址:https://www.cnblogs.com/dramstadt/p/6207396.html