AtCoder Beginnner Contest 070

vi还没配置(旧电脑,不想再折腾啦),空格缩进不合格,,请见谅

A(三位数,取第一位和最后一位)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4         int n;
 5         scanf("%d",&n);
 6         int x=n/100;
 7         int y=n%10;
 8         if(x==y) printf("Yes
");
 9         else printf("No
");
10         return 0;
11 }

B  (可以联想成求两个集合的交集)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4         int a,b,m,n;
 5         scanf("%d%d%d%d",&a,&b,&m,&n);
 6         if(b<=m){
 7         return !printf("0
");
 8         }else{
 9         if(a<m){
10         if(b<=n){
11         printf("%d
",b-m);
12                         }else {
13         printf("%d
",n-m);
14                         }
15                 }else {
16                 if(n>a){
17                 if(n<=b) return !printf("%d
",n-a);
18                 else return!printf("%d
",b-a);
19                         } else return !printf("0
");
20 
21                 }
22         }
23 }

C(求最大公倍数-->两个数相乘除以两个数的最大公约数就是最小公倍数,给n个数求n个数的最小公倍数,只能遍历啦 )

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long int
 4 ll gcd(ll  a,ll b){
 5         return b?gcd(b,a%b):a;
 6 }
 7 
 8 ll  a[110];
 9 int main(){
10         int n;
11         scanf("%d",&n);
12         for(int i=0;i<n;i++)
13         cin>>a[i];
14         ll ans=a[0];
15         for(int i=1;i<n;i++){
16          ans=ans/gcd(ans,a[i])*a[i];
17         }
18         return !printf("%lld
",ans);
19 }

D - Transit Tree Path


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

You are given a tree with N vertices.
Here, a tree is a kind of graph, and more specifically, a connected undirected graph with N1 edges, where N is the number of its vertices.
The i-th edge (1iN1) connects Vertices ai and bi, and has a length of ci.

You are also given Q queries and an integer K. In the j-th query (1jQ):

  • find the length of the shortest path from Vertex xj and Vertex yj via Vertex K.

Constraints

  • 3N105
  • 1ai,biN(1iN1)
  • 1ci109(1iN1)
  • The given graph is a tree.
  • 1Q105
  • 1KN
  • 1xj,yjN(1jQ)
  • xjyj(1jQ)
  • xjK,yjK(1jQ)

Input

Input is given from Standard Input in the following format:

N  
a1 b1 c1  
:  
aN1 bN1 cN1
Q K
x1 y1
:  
xQ yQ

Output

Print the responses to the queries in Q lines.
In the j-th line j(1jQ), print the response to the j-th query.


Sample Input 1

Copy
5
1 2 1
1 3 1
2 4 1
3 5 1
3 1
2 4
2 3
4 5

Sample Output 1

Copy
3
2
4

The shortest paths for the three queries are as follows:

  • Query 1: Vertex 2 → Vertex 1 → Vertex 2 → Vertex 4 : Length 1+1+1=3
  • Query 2: Vertex 2 → Vertex 1 → Vertex 3 : Length 1+1=2
  • Query 3: Vertex 4 → Vertex 2 → Vertex 1 → Vertex 3 → Vertex 5 : Length 1+1+1+1=4

Sample Input 2

Copy
7
1 2 1
1 3 3
1 4 5
1 5 7
1 6 9
1 7 11
3 2
1 3
4 5
6 7

Sample Output 2

Copy
5
14
22

The path for each query must pass Vertex K=2.


Sample Input 3

Copy
10
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
6 7 1000000000
7 8 1000000000
8 9 1000000000
9 10 1000000000
1 1
9 10

Sample Output 3

Copy
17000000000

题意:给定一个N,代表有N个节点,构成一个具有N-1条边的无向图,节点之间的边带权值,因为

  • xjyj(1jQ)这个条件,没有圈,所以严格上说是一颗树
  • Q次查询,每次查询必须经过K,问每次查询的最短路径是多少
 1     #include <stdio.h>
 2     #include <string.h>
 3     #include <queue>
 4     #include <vector>
 5     using namespace std;
 6      
 7     struct edge
 8     {
 9         int from,to;
10         int cost;
11     };
12      
13     long long d[100005];
14     bool vis[100005];
15      
16     vector<edge> edges;
17     vector<int> v[100005];
18      
19     void adde(int from,int to,int cost)
20     {
21         edge e;
22         e.from = from;
23         e.to = to;
24         e.cost = cost;
25      
26         edges.push_back(e);
27      
28         int m = edges.size() - 1;
29      
30         v[from].push_back(m);
31     }
32      
33     void bfs(int s)
34     {
35         vis[s] = 1;
36         d[s] = 0;
37      
38         queue<int> q;
39      
40         q.push(s);
41      
42         while (!q.empty())
43         {
44             int now = q.front();q.pop();
45      
46             for (int i = 0;i < v[now].size();i++)
47             {
48                 int id = v[now][i];
49      
50                 edge tmp = edges[id];
51      
52                 if (!vis[tmp.to])
53                 {
54                     vis[tmp.to] = 1;
55      
56                     d[tmp.to] = d[now] + tmp.cost;
57      
58                     q.push(tmp.to);
59                 }
60             }
61         }
62     }
63      
64     int main()
65     {
66         int n;
67      
68         scanf("%d",&n);
69      
70         for (int i = 1;i <= n - 1;i++)
71         {
72             int a,b,c;
73      
74             scanf("%d%d%d",&a,&b,&c);
75      
76             adde(a,b,c);
77             adde(b,a,c);
78         }
79      
80         int q,k;
81      
82         scanf("%d%d",&q,&k);
83      
84         bfs(k);
85      
86         for (int i = 0;i < q;i++)
87         {
88             int a,b;
89      
90             scanf("%d%d",&a,&b);
91      
92             printf("%lld
",d[a] + d[b]);
93         }
94      
95         return 0;
96     }
原文地址:https://www.cnblogs.com/z-712/p/7358898.html