Codeforces Round #303 (Div. 2)

http://codeforces.com/contest/545/problem/E
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·1050?≤?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 3
1 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
Note

In the first sample there are two possible shortest path trees:

  • with edges 1?–?3 and 2?–?3 (the total weight is 3);
  • with edges 1?–?2 and 2?–?3 (the total weight is 2);

And, for example, a tree with edges 1?–?2 and 1?–?3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1.

题目大意:
给出一个n个顶点的m条边的带权图,给定起点u,求出从起点开始,到每个顶点的权值和的最小值。
Codeforces Round 303 (Div. 2) - 风未定 - NGUNAUJ
 第一个样例, 选3->2  2->1 总权重为2  使用到的边为 第1条边 第2条边
这个求解的过程和dijistra算法的求解一致,可用dijistra算法求解,记录用到的边。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define maxn 300000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
const ll INF=1LL<<60;
using namespace std;

bool cmp(ll a,ll b){
return a>b;
}
ll n,m;
struct edge{
ll u,v,id;
ll w;
edge(ll uu,ll vv,ll ww,ll ID){
u=uu;v=vv;w=ww;id=ID;
}
};
typedef pair<ll,int>pii;
ll lowcost[maxn];
ll sum;
ll ex[maxn];
int pre[maxn];
bool vis[maxn];
vector<int>v;
void dij(int s,vector<edge>G[]){
for(int i=1;i<=n;i++){
lowcost[i]=(i==s?0:INF);
ex[i]=INF;
}
priority_queue<pii>q;
v.clear();

q.push(make_pair(0,s));
int k=s;
while(!q.empty()){
pii u=q.top();q.pop();
int x=u.second;
if(vis[x])continue;
//已经更新过
vis[x]=1;

for(ll i=0;i<G[x].size();i++){
ll y=G[x][i].v;
ll w=G[x][i].w;
ll id=G[x][i].id;
if(lowcost[y]>lowcost[x]+w){
lowcost[y]=lowcost[x]+w;
ex[y]=w;pre[y]=id;
q.push(make_pair(-lowcost[y],y));
}
else if(lowcost[y]==lowcost[x]+w&&ex[y]>w){
ex[y]=w;pre[y]=id;
}
}
}
sum=0;
for(int i=1;i<=n;i++){
if(ex[i]!=INF)sum+=ex[i];
if(i!=s)v.push_back(pre[i]);
}
cout<<sum<<endl;
//unique(v.begin(),v.end());//去掉重复的
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
}
vector<edge>G[maxn];//邻接表
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n>>m){
for(int i=0;i<maxn;i++)G[i].clear();
int a,b;ll c;
for(int i=1;i<=m;i++){
scanf("%d %d %I64d",&a,&b,&c);
edge E1(a,b,c,i);
edge E2(b,a,c,i);
G[a].push_back(E1);
G[b].push_back(E2);
}
int s;
sum=0;
scanf("%d",&s);
dij(s,G);
}
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
Note

Value 4 is achieved at such an arrangement, for example: 1,?2,?3,?5,?15. Thus, you can make everything feel not disappointed except for the person with time 5.

题目大意:n个人排队shopping,如果某个人在规定的a[i]内没有开始shopping 那么他就会失望,求最小的失望人数
思路:a[i]小的尽量往前排。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 100000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int a[maxn];
int main()
{
    #ifndef ONLINE_JUDGE
    //freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
	//1 2 5 7
	//5 4 6 7
	//1 1 1 2
    int n;
	while(cin>>n){
		for(int i=0;i<n;i++)
			cin>>a[i];
		sort(a,a+n);
		ll sum=0,num=0;
		for(int i=0;i<n;i++){
			if(a[i]>=sum){
				num++;
				sum+=a[i];
			}
		}
		cout<<num<<endl;
	}
	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
Note

In the first sample you can fell the trees like that:

  • fell the 1-st tree to the left — now it occupies segment [?-?1;1]
  • fell the 2-nd tree to the right — now it occupies segment [2;3]
  • leave the 3-rd tree — it occupies point 5
  • leave the 4-th tree — it occupies point 10
  • fell the 5-th tree to the right — now it occupies segment [19;20]

In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].

题目大意:有n棵树,给定每棵树的高度,砍树后,树可以向左倒也可以向右倒,如果某棵树倒下了,它所覆覆盖的坐标中有其他的树(包括已经倒下的树的坐标),那么这棵树就不能砍,求最多能砍多少棵树。
思路:模拟。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1LL<<60
#define maxn 100000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
return a>b;
}
pair<ll,ll>p[maxn];
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n;
while(cin>>n){
for(int i=1;i<=n;i++)
cin>>p[i].first>>p[i].second;
ll k=-INF;
int num=0;
for(int i=1;i<=n;i++){
if(p[i].first-p[i].second>k){
k=p[i].first;
num++;
}
else if(i==n||p[i].first+p[i].second<p[i+1].first){
num++;
k=p[i].first+p[i].second;
}
else k=p[i].first;
}
cout<<num<<endl;
}
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.

题目大意:给定s 串 t 串  要求找到一个p串,使得p与s的不同字符的数目与p与t的不同字符数目相同
s:0001  t:1011  p:0011  p与s对应位的不同数目为 2   p与t对应位的不同数目为2    p符合要求
解题思路:找出s 与t 的对应位不同的数量 cnt,如果cnt为奇数,那么由于串的特点(01串)我们找不到符合的p 
如果cnt 为偶数, 那么我们可以取 s的前cnt/2个不同给p的前cnt/2位,取t的后cnt/2个不同给p的后cnt/2位。那么p就找到了。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
string s,t,p;
while(cin>>s>>t){
int cnt=0;
for(int i=0;i<s.size();i++){
if(s[i]!=t[i])cnt++;
}
if(cnt%2)puts("impossible");
else{
cnt/=2;
for(int i=0;i<s.size();i++){
if(s[i]!=t[i])if(cnt>0)cout<<s[i],cnt--;
else cout<<t[i];
else cout<<s[i];
}
}
}
return 0;
}

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
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
矩阵模拟一下就好了
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
int a[110][110];
int num;
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n ;
while(cin>>n){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
int broke[110];
cle(broke);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(a[i][j]==1)broke[i]=1;
if(a[i][j]==2)broke[j]=1;
if(a[i][j]==3){broke[i]=1;broke[j]=1;}
}
num=0;
for(int i=1;i<=n;i++)
if(broke[i]==0)num++;
cout<<num<<endl;
for(int i=1;i<=n;i++)
if(broke[i]==0)cout<<i<<" ";
}
return 0;
}

 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值