计科练习13题解(最短路径)

目录

XP的点滴:

丛林小道

How Many Tables 

Dijkstra

寻找最大数

最短路径问题


XP的点滴:

https://blog.youkuaiyun.com/Y_yunhu/article/details/84714635

丛林小道

题目描述


The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

输入

 

9 A 2 B 12 I 25 B 3 C 10 H 40 I 8 C 2 D 18 G 55 D 1 E 44 E 2 F 60 G 38 F 0 G 1 H 35 H 1 I 35 3 A 2 B 10 C 40 B 1 C 20 0

输出

 

216 30

样例输入 Copy

9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0

样例输出 Copy

216
30
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
typedef struct mystruct{
    int sta,fin;
    int v;
};
mystruct e[1005];
int fa[1005];
int r[1005];
int n,m,ans,sum;
bool cmp(mystruct a,mystruct b){
    return a.v<b.v;
}
void mset(){
    for(int i=0;i<=ans;++i){
        fa[i]=i;
        r[i]=0;
    }
}
int fset(int x){
    if(fa[x]!=x){
        fa[x]=fset(fa[x]);
    }
    return fa[x];
}
void Krusal(){
    int i,j;
    mset();
    for(i=0;i<ans;++i){
        int aa=fset(e[i].sta);
        int bb=fset(e[i].fin);
        if(aa==bb){
            continue;
        }
        if(r[aa]>r[bb]){
            fa[bb]=fa[aa];
        }else{
            fa[aa]=fa[bb];
            if(r[aa]==r[bb]){
                ++r[bb];
            }
        }
        sum+=e[i].v;
    }
}
int main(){
    int t;
    int i,j;
    char x,y;
    int a,b;
    while(cin>>t&&t){
        ans=0;
        sum=0;
        for(i=0;i<t-1;++i){
            cin>>x>>a;
            for(j=0;j<a;++j){
                cin>>y>>b;
                e[ans].sta=x-'A';
                e[ans].fin=y-'A';
                e[ans++].v=b;
            }
        }
        sort(e,e+ans,cmp);
        Krusal();
        cout<<sum<<endl;
    }
	return 0;
}

How Many Tables 

题目描述

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

输入

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

输出

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

样例输入 Copy

2
5 3
1 2
2 3
4 5

5 1
2 5

样例输出 Copy

2
4

#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
int fa[1005],r[1005];
int n,m,ans;
void mset(){
    for(int i=0;i<=n;++i){
        fa[i]=i;
        r[i]=0;
    }
}
int fset(int x){
    if(fa[x]!=x){
        fa[x]=fset(fa[x]);
    }
    return fa[x];
}
void uset(int a,int b){
    int aa=fset(a);
    int bb=fset(b);
    if(aa==bb){
        return ;
    }
    ans--;
    if(r[aa]>r[bb]){
        fa[bb]=fa[aa];
    }else{
        fa[aa]=fa[bb];
        if(r[aa]==r[bb]){
            ++r[aa];
        }
    }
}
int main(){
    int t;
    int i,j;
    while(cin>>t){
        while(t--){
            int x,y;
            cin>>n>>m;
            mset();
            ans=n;
            for(i=0;i<m;++i){
                cin>>x>>y;
                uset(x,y);
            }
            cout<<ans<<endl;
        }
    }
	return 0;
}

Dijkstra

题目描述

编程实现Dijkstra算法

输入

第1行第1个值表示顶点个数,第2个值表示边个数;第2行开始为边及权重

输出

顶点0到每一个顶点的最短路径长度

样例输入 Copy

5 7
0 1 10
0 3 30
0 4 100
1 2 50
2 4 10
3 2 20
3 4 60

样例输出 Copy

0 10 50 30 60
#include<bits/stdc++.h>
using namespace std;
#define maxn 99999999
int e[1005][1005];
int vis[1005];
int dis[1005];
int n, m;
int u;
void mset() {
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j) {
        e[i][j] = 0;
      } else {
        e[i][j] = maxn;
      }
    }
  }
}
void dijkstra() {
  int i, j;
  for (i = 1; i <= n - 1; i++) {
    int Min = maxn;
    // 寻找权值最小的点u
    for (j = 1; j <= n; j++) {
      if (vis[j] == 0 && dis[j] < Min) {
        Min = dis[j];
        u = j;
      }
    }
    vis[u] = 1;
    for (j = 1; j <= n; j++) {
      if (!vis[j] && e[u][j] < maxn) {
        if (dis[j] > dis[u] + e[u][j]) {
          dis[j] = dis[u] + e[u][j];
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << dis[i] << " ";
  }
  cout << endl;
}
int main() {
  int a, b, c;
  int i;
  while (cin >> n >> m) {
    u = 0;
    mset();
    for (i = 1; i <= m; i++) {
      cin >> a >> b >> c;
      ++a;
      ++b;
      e[a][b] = c;
    }
    for (i = 1; i <= n; i++) {
      dis[i] = e[1][i];
    }
    vis[1] = 1;
    dijkstra();
  }
  return 0;
}

寻找最大数

题目描述

给出一个正整数N,每次可以移动2个相邻数位上的数字,最多移动K次,得到一个新的正整数。求这个新的正整数的最大值。

输入

输入一个正整数N和K,输出新的正整数。例如:N=1990,K=1,输出9190;N=101,K=0,输出101;N= 9090000078001234,K= 6,输出9907000008001234。

输出

输出新的数字

 

样例输入 Copy

1990 1
101 0
9090000078001234 6

样例输出 Copy

9190
101
9907000008001234
#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    int n;
    int i,j;
    int len;
    while(cin>>s>>n){
        int ans=n;
        int index;
        index=0;
        len=s.length();
        if(len==1||n==0){
            cout<<s<<endl;
            continue;
        }
        for(i=0;i<len&&ans!=0;++i){
            char maxc=s[i];
            for(j=i+1;j<i+ans+1&&j<len;++j){
                if(s[j]>maxc){
                    index=j;
                    maxc=s[j];
                }
            }
            if(maxc!=s[i]){
                for(j=index;j>i;j--){
                    swap(s[j],s[j-1]);
                }
                ans=ans-index+i;
            }
        }
        cout<<s<<endl;
    }
	return 0;
}

最短路径问题

题目描述

给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。

输入

输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束,(n <= 100,保证给定的图一定是连通的)

输出

输出 一行有两个数, 最短距离及其花费。

样例输入 Copy

3 2
1 2 5 6
2 3 4 5
1 3
0 0

样例输出 Copy

9 11
#include<bits/stdc++.h>
using namespace std;
#define maxn 99999999
int e[1005][1005];
int vis[1005];
int dis[1005];
int sum[1005];
int val[1005][1005];
int n, m,s,t;
void mset() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == j) {
                e[i][j] = 0;
                val[i][j]=0;
            } else {
                e[i][j] = maxn;
                val[i][j]=maxn;
            }
        }
    }
}
void dijkstra() {
    int i, j;
    memset(vis, 0, sizeof(vis));
    vis[s] = 1;
    dis[s]=0;
    sum[s]=0;
    for (i = 1; i <= n; i++) {
      dis[i] = e[s][i];
      sum[i]=val[s][i];
    }
    int u;
    for (i = 1; i < n; i++) {
        int Min = maxn;
        for (j = 1; j <= n; j++) {
            if (vis[j] == 0 && dis[j] < Min) {
                Min = dis[j];
                u = j;
            }
        }
        vis[u] = 1;
        for (j = 1; j <= n; j++) {
            if (vis[j]==0 && e[u][j] < maxn) {
                if (dis[j] > dis[u] + e[u][j]) {
                    dis[j] = dis[u] + e[u][j];
                    sum[j] = sum[u] + val[u][j];
                }else if(dis[j] == dis[u] + e[u][j]&&sum[j] > sum[u] + val[u][j]){
                    sum[j] = sum[u] + val[u][j];
                }
            }
        }
    }
    cout<<dis[t]<<" "<<sum[t]<<endl;
}
int main() {
    int a, b, c,d;
    int i;
    while (cin >> n >> m&&(m||n)) {
        mset();
        for (i = 1; i <= m; i++) {
            cin >> a >> b >> c>>d;
            if(e[a][b]>c){
                e[a][b] =e[b][a]= c;
                val[a][b]=val[b][a]=d;
            }else if(e[a][b]==c&&val[a][b]>d){
                val[a][b]=val[b][a]=d;
            }
        }
        cin>>s>>t;
        dijkstra();
    }
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值