Codeforces #303 (div2)
2015-05-29 00:29
253 查看
A. Toy Cars
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph.
There are n toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned
over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an n × n matrix А:
there is a number on the intersection of the і-th row and j-th
column that describes the result of the collision of the і-th and the j-th
car:
- 1: if this pair of cars never collided. - 1 occurs
only on the main diagonal of the matrix.
0: if no car turned over during the collision.
1: if only the i-th
car turned over during the collision.
2: if only the j-th
car turned over during the collision.
3: if both cars turned over during the collision.
Susie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task?
Input
The first line contains integer n (1 ≤ n ≤ 100)
— the number of cars.
Each of the next n lines contains n space-separated
integers that determine matrix A.
It is guaranteed that on the main diagonal there are - 1, and - 1 doesn't
appear anywhere else in the matrix.
It is guaranteed that the input is correct, that is, if Aij = 1,
then Aji = 2,
if Aij = 3,
then Aji = 3,
and if Aij = 0,
then Aji = 0.
Output
Print the number of good cars and in the next line print their space-separated indices in the increasing order.
Sample test(s)
input
output
input
output
嘛~ 照题意统计一下就好啦~
AC代码如下:
//
// Created by TaoSama on 2015-05-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n;
bool over[105];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(cin >> n) {
memset(over, false, sizeof over);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
int x; cin >> x;
if(x == 1) over[i] = true;
else if(x == 2) over[j] = true;
else if(x == 3) over[i] = over[j] = true;
}
}
int ans = 0;
for(int i = 1; i <= n; ++i) if(!over[i]) ++ans;
cout << ans << '\n';
for(int i = 1; i <= n; ++i)
if(!over[i]) cout << i << ' ';
cout << '\n';
}
return 0;
}
B. Equidistant String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:
We will define the distance between two strings s and t of
the same length consisting of digits zero and one as the number of positions i, such that si isn't
equal to ti.
As besides everything else Susie loves symmetry, she wants to find for two strings s and t of
length n such string p of
length n, that the distance from p to s was
equal to the distance from p to t.
It's time for Susie to go to bed, help her find such string p or state that it is impossible.
Input
The first line contains string s of length n.
The second line contains string t of length n.
The length of string n is within range from 1 to 105.
It is guaranteed that both strings contain only digits zero and one.
Output
Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on
a single line "impossible" (without the quotes).
If there are multiple possible answers, print any of them.
Sample test(s)
input
output
input
output
Note
In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.
嘛~ 仔细读题之后发现这题可以贪心 对于相同的字母就不管他~ 不同的就 上下各分一个~ 如果是奇数那就不行啦~
AC代码如下:
//
// Created by TaoSama on 2015-05-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
string s, t;
char ans
;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(cin >> s >> t) {
int cnt = 0;
for(int i = 0; i < s.size(); ++i) {
if(s[i] != t[i]) {
++cnt;
ans[i] = (cnt & 1) ? s[i] : t[i];
} else ans[i] = s[i];
}
if(cnt & 1) cout << "impossible\n";
else {
ans[s.size()] = 0;
puts(ans);
}
}
return 0;
}
C. Woodcutters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.
There are n trees located along the road at points with coordinates x1, x2, ..., xn.
Each tree has its height hi.
Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi].
The tree that is not cut down occupies a single point with coordinate xi.
Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.
Input
The first line contains integer n (1 ≤ n ≤ 105)
— the number of trees.
Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109)
— the coordinate and the height of the і-th tree.
The pairs are given in the order of ascending xi.
No two trees are located at the point with the same coordinate.
Output
Print a single number — the maximum number of trees that you can cut down by the given rules.
Sample test(s)
input
output
input
output
贪心专场 - - 看这颗树能不能倒 反正不是这个倒 就是旁边那个倒 如果二者只能倒一的话 那个倒都是一样的 所以贪心策略 我们二选一
选择先让左边的倒 然后最后一个是一定可以倒的就可以了
AC代码如下:
//
// Created by TaoSama on 2015-05-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n, l
, r
;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d", &n) == 1) {
for(int i = 1; i <= n; ++i) {
scanf("%d%d", l + i, r + i);
}
int ans = 1, t = -INF;
for(int i = 1; i < n; ++i) {
if(l[i] - r[i] > t) {
++ans;
t = l[i];
} else {
if(l[i] + r[i] < l[i + 1]) {
t = l[i] + r[i];
++ans;
} else t = l[i];
}
//cout<<i<<':'<<t<<endl;
}
cout << ans << '\n';
}
return 0;
}
D. Queue
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Little girl Susie went shopping with her mom and she wondered how to improve service quality.
There are n people in the queue. For each person we know time ti needed
to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some
people in the queue, then we can decrease the number of people who are disappointed.
Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.
Input
The first line contains integer n (1 ≤ n ≤ 105).
The next line contains n integers ti (1 ≤ ti ≤ 109),
separated by spaces.
Output
Print a single number — the maximum number of not disappointed people in the queue.
Sample test(s)
input
output
让小的排在前面比大的排在前面更优 - - 所以这题的贪心策略也很简单
就是sort一下就好了- -
AC代码如下:
//
// Created by TaoSama on 2015-05-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n, dp
[2], a
;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
sort(a + 1, a + 1 + n);
int sum = 0, ans = 0;
for(int i = 1; i <= n; ++i) {
if(sum <= a[i]) {
sum += a[i];
++ans;
}
}
cout << ans << '\n';
return 0;
}
E. Paths and Trees
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem
statement is as follows.
Let's assume that we are given a connected weighted undirected graph G = (V, E) (here V is
the set of vertices, E is the set of edges). The shortest-path tree from vertex u is
such graph G1 = (V, E1) that
is a tree with the set of edges E1 that
is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to
any vertex to G and to G1 are
the same.
You are given a connected weighted undirected graph G and vertex u.
Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible.
Input
The first line contains two numbers, n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105)
— the number of vertices and edges of the graph, respectively.
Next m lines contain three integers each, representing an edge — ui, vi, wi —
the numbers of vertices connected by an edge and the weight of the edge (ui ≠ vi, 1 ≤ wi ≤ 109).
It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.
The last line of the input contains integer u (1 ≤ u ≤ n)
— the number of the start vertex.
Output
In the first line print the minimum total weight of the edges of the tree.
In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from1 in
the order they follow in the input. You may print the numbers of the edges in any order.
If there are multiple answers, print any of them.
Sample test(s)
input
output
input
output
这个题之前AOJ上做过一个类似的 - - 我们只要在跑最短路的时候 记录下最小花费的边就好了 - - 并记录下它的编号
AC代码如下:
//
// Created by TaoSama on 2015-05-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 3e5 + 10;
int n, m;
struct E {
int id, v, cost;
};
typedef pair<long long, int> P;
vector<E> G
;
long long d
;
int minCost
, ed
;
bool used
;
long long dijkstra(int s) {
memset(d, 0x3f, sizeof d);
memset(minCost, 0x3f, sizeof minCost);
//memset(used, false, sizeof used);
d[s] = minCost[s] = 0;
priority_queue<P, vector<P>, greater<P> > pq;
pq.push(P(0, s));
long long ret = 0;
while(!pq.empty()) {
P p = pq.top(); pq.pop();
int u = p.second;
long long dis = p.first;
if(dis > d[u]) continue;
//used[u] = true;
ret += minCost[u];
for(int i = 0; i < G[u].size(); ++i) {
E &e = G[u][i];
if(d[e.v] > d[u] + e.cost) {
d[e.v] = d[u] + e.cost;
minCost[e.v] = e.cost;
ed[e.v] = e.id;
pq.push(P(d[e.v], e.v));
} else if(d[e.v] == d[u] + e.cost &&
minCost[e.v] > e.cost) {
minCost[e.v] = e.cost;
ed[e.v] = e.id;
}
}
}
return ret;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d%d", &n, &m) == 2) {
for(int i = 1; i <= n; ++i) G[i].clear();
for(int i = 1; i <= m; ++i) {
int x, y, z; scanf("%d%d%d", &x, &y, &z);
G[x].push_back((E) {i, y, z});
G[y].push_back((E) {i, x, z});
}
int s; scanf("%d", &s);
long long ans = dijkstra(s);
printf("%I64d\n", ans);
for(int i = 1; i <= n; ++i)
if(i != s) printf("%d ", ed[i]);
puts("");
}
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph.
There are n toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned
over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an n × n matrix А:
there is a number on the intersection of the і-th row and j-th
column that describes the result of the collision of the і-th and the j-th
car:
- 1: if this pair of cars never collided. - 1 occurs
only on the main diagonal of the matrix.
0: if no car turned over during the collision.
1: if only the i-th
car turned over during the collision.
2: if only the j-th
car turned over during the collision.
3: if both cars turned over during the collision.
Susie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task?
Input
The first line contains integer n (1 ≤ n ≤ 100)
— the number of cars.
Each of the next n lines contains n space-separated
integers that determine matrix A.
It is guaranteed that on the main diagonal there are - 1, and - 1 doesn't
appear anywhere else in the matrix.
It is guaranteed that the input is correct, that is, if Aij = 1,
then Aji = 2,
if Aij = 3,
then Aji = 3,
and if Aij = 0,
then Aji = 0.
Output
Print the number of good cars and in the next line print their space-separated indices in the increasing order.
Sample test(s)
input
3 -1 0 0 0 -1 1 0 2 -1
output
2 1 3
input
4 -1 3 3 3 3 -1 3 3 3 3 -1 3 3 3 3 -1
output
0
嘛~ 照题意统计一下就好啦~
AC代码如下:
//
// Created by TaoSama on 2015-05-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n;
bool over[105];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(cin >> n) {
memset(over, false, sizeof over);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
int x; cin >> x;
if(x == 1) over[i] = true;
else if(x == 2) over[j] = true;
else if(x == 3) over[i] = over[j] = true;
}
}
int ans = 0;
for(int i = 1; i <= n; ++i) if(!over[i]) ++ans;
cout << ans << '\n';
for(int i = 1; i <= n; ++i)
if(!over[i]) cout << i << ' ';
cout << '\n';
}
return 0;
}
B. Equidistant String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:
We will define the distance between two strings s and t of
the same length consisting of digits zero and one as the number of positions i, such that si isn't
equal to ti.
As besides everything else Susie loves symmetry, she wants to find for two strings s and t of
length n such string p of
length n, that the distance from p to s was
equal to the distance from p to t.
It's time for Susie to go to bed, help her find such string p or state that it is impossible.
Input
The first line contains string s of length n.
The second line contains string t of length n.
The length of string n is within range from 1 to 105.
It is guaranteed that both strings contain only digits zero and one.
Output
Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on
a single line "impossible" (without the quotes).
If there are multiple possible answers, print any of them.
Sample test(s)
input
0001 1011
output
0011
input
000 111
output
impossible
Note
In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.
嘛~ 仔细读题之后发现这题可以贪心 对于相同的字母就不管他~ 不同的就 上下各分一个~ 如果是奇数那就不行啦~
AC代码如下:
//
// Created by TaoSama on 2015-05-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
string s, t;
char ans
;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(cin >> s >> t) {
int cnt = 0;
for(int i = 0; i < s.size(); ++i) {
if(s[i] != t[i]) {
++cnt;
ans[i] = (cnt & 1) ? s[i] : t[i];
} else ans[i] = s[i];
}
if(cnt & 1) cout << "impossible\n";
else {
ans[s.size()] = 0;
puts(ans);
}
}
return 0;
}
C. Woodcutters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.
There are n trees located along the road at points with coordinates x1, x2, ..., xn.
Each tree has its height hi.
Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi].
The tree that is not cut down occupies a single point with coordinate xi.
Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.
Input
The first line contains integer n (1 ≤ n ≤ 105)
— the number of trees.
Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109)
— the coordinate and the height of the і-th tree.
The pairs are given in the order of ascending xi.
No two trees are located at the point with the same coordinate.
Output
Print a single number — the maximum number of trees that you can cut down by the given rules.
Sample test(s)
input
5 1 2 2 1 5 10 10 9 19 1
output
3
input
5 1 2 2 1 5 10 10 9 20 1
output
4
贪心专场 - - 看这颗树能不能倒 反正不是这个倒 就是旁边那个倒 如果二者只能倒一的话 那个倒都是一样的 所以贪心策略 我们二选一
选择先让左边的倒 然后最后一个是一定可以倒的就可以了
AC代码如下:
//
// Created by TaoSama on 2015-05-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n, l
, r
;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d", &n) == 1) {
for(int i = 1; i <= n; ++i) {
scanf("%d%d", l + i, r + i);
}
int ans = 1, t = -INF;
for(int i = 1; i < n; ++i) {
if(l[i] - r[i] > t) {
++ans;
t = l[i];
} else {
if(l[i] + r[i] < l[i + 1]) {
t = l[i] + r[i];
++ans;
} else t = l[i];
}
//cout<<i<<':'<<t<<endl;
}
cout << ans << '\n';
}
return 0;
}
D. Queue
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Little girl Susie went shopping with her mom and she wondered how to improve service quality.
There are n people in the queue. For each person we know time ti needed
to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some
people in the queue, then we can decrease the number of people who are disappointed.
Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.
Input
The first line contains integer n (1 ≤ n ≤ 105).
The next line contains n integers ti (1 ≤ ti ≤ 109),
separated by spaces.
Output
Print a single number — the maximum number of not disappointed people in the queue.
Sample test(s)
input
5
15 2 1 5 3
output
4
让小的排在前面比大的排在前面更优 - - 所以这题的贪心策略也很简单
就是sort一下就好了- -
AC代码如下:
//
// Created by TaoSama on 2015-05-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n, dp
[2], a
;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
sort(a + 1, a + 1 + n);
int sum = 0, ans = 0;
for(int i = 1; i <= n; ++i) {
if(sum <= a[i]) {
sum += a[i];
++ans;
}
}
cout << ans << '\n';
return 0;
}
E. Paths and Trees
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem
statement is as follows.
Let's assume that we are given a connected weighted undirected graph G = (V, E) (here V is
the set of vertices, E is the set of edges). The shortest-path tree from vertex u is
such graph G1 = (V, E1) that
is a tree with the set of edges E1 that
is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to
any vertex to G and to G1 are
the same.
You are given a connected weighted undirected graph G and vertex u.
Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible.
Input
The first line contains two numbers, n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105)
— the number of vertices and edges of the graph, respectively.
Next m lines contain three integers each, representing an edge — ui, vi, wi —
the numbers of vertices connected by an edge and the weight of the edge (ui ≠ vi, 1 ≤ wi ≤ 109).
It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.
The last line of the input contains integer u (1 ≤ u ≤ n)
— the number of the start vertex.
Output
In the first line print the minimum total weight of the edges of the tree.
In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from1 in
the order they follow in the input. You may print the numbers of the edges in any order.
If there are multiple answers, print any of them.
Sample test(s)
input
3 31 2 1
2 3 1
1 3 2
3
output
2 1 2
input
4 4 1 2 1 2 3 1 3 4 1 4 1 2 4
output
4 2 3 4
这个题之前AOJ上做过一个类似的 - - 我们只要在跑最短路的时候 记录下最小花费的边就好了 - - 并记录下它的编号
AC代码如下:
//
// Created by TaoSama on 2015-05-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 3e5 + 10;
int n, m;
struct E {
int id, v, cost;
};
typedef pair<long long, int> P;
vector<E> G
;
long long d
;
int minCost
, ed
;
bool used
;
long long dijkstra(int s) {
memset(d, 0x3f, sizeof d);
memset(minCost, 0x3f, sizeof minCost);
//memset(used, false, sizeof used);
d[s] = minCost[s] = 0;
priority_queue<P, vector<P>, greater<P> > pq;
pq.push(P(0, s));
long long ret = 0;
while(!pq.empty()) {
P p = pq.top(); pq.pop();
int u = p.second;
long long dis = p.first;
if(dis > d[u]) continue;
//used[u] = true;
ret += minCost[u];
for(int i = 0; i < G[u].size(); ++i) {
E &e = G[u][i];
if(d[e.v] > d[u] + e.cost) {
d[e.v] = d[u] + e.cost;
minCost[e.v] = e.cost;
ed[e.v] = e.id;
pq.push(P(d[e.v], e.v));
} else if(d[e.v] == d[u] + e.cost &&
minCost[e.v] > e.cost) {
minCost[e.v] = e.cost;
ed[e.v] = e.id;
}
}
}
return ret;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d%d", &n, &m) == 2) {
for(int i = 1; i <= n; ++i) G[i].clear();
for(int i = 1; i <= m; ++i) {
int x, y, z; scanf("%d%d%d", &x, &y, &z);
G[x].push_back((E) {i, y, z});
G[y].push_back((E) {i, x, z});
}
int s; scanf("%d", &s);
long long ans = dijkstra(s);
printf("%I64d\n", ans);
for(int i = 1; i <= n; ++i)
if(i != s) printf("%d ", ed[i]);
puts("");
}
return 0;
}
相关文章推荐
- java多线程(二):线程的实现
- 自定义PopupWindow+xml布局+Anim
- 浅谈Docker隔离性和安全性
- leetcode:Validate Binary Search Tree
- 使用git submodule管理一个需要多个分立开发或者第三方repo的项目
- Kphone 4.2 在ok6410上的移植手册
- 接口、抽象类、框架
- LVS负载均衡LAMP平台
- 自定义PopupWindow+SimpleAdapter+Anim
- CAS工作原理
- File类
- SharePoint 2013 workflow cannot start automatically when you logged in site as a system account
- springmvc4
- UVA 11827 - Maximum GCD
- 关于二维数组和指针
- Linux文档与目录
- ant 的实用例子
- 阅读计划3---《大道至简》
- 数据库优化知识
- vim 查找命令