9-13(周三训练赛-思路染色,费用流二分图Point)

先说一下最大流最小费用

其实只是把EdmonsKarp网络流中的DFS换成Bellman Ford而已
注意区分+理解
网络流判断 : if(!a[e.to] && e.cap>e.flow)
费用流判断: if(e.cap>e.flow && d[e.to] > d[u]+e.cost)

再提一下二分匹配

了解概念: 完美匹配, 最大权完美匹配, 最大基数匹配
理解+熟悉特点
一个图是二分图,当且仅当这个图中不含长度为奇数的圈
PS:在不知道这个关系的情况下,你会怎么推出来?

今天训练赛的几道题:

Comunicating the Tibet********

time limit per test2.0 s
memory limit per test512 MB

The Himalayas form the highest mountain range in the world (with mountains as high as 7,200 meters). Located in Asia, this mountain system has parts in the Bhutan, China, India, Nepal, and Pakistan. In China, the Tibet is a region delimited by the Himalayas.

The Tibet, which is the highest region on Earth, is an autonomous region inside the People’s Republic of China. In this region there are Buddhist temples with Tibetan monks, which are visited every year by many people from the whole world. To facilitate communication, the Central Tibetan Administration (ACT, in Portuguese) wishes to install some radio-frequency antennas around the Tibet. The ACT considered the N most important temples where they wish to install these antennas. The World Communication Association (ACM, in Portuguese) was hired to setup and configure these antennas at the temples. ACM charged their star engineer Mashiro “thunderstorm” Sanae with this job.

After reading about the history and customs of Tibetan monks, Mashiro found a curious fact: the Tibetans have a particular fixation for the number K. By studying the map with the location of the N temples, Mashiro noticed that if he walks through distinct temples until he returns to the starting one, the number of temples he goes through (including the first) never has the form mK + 1 for any nonnegative integer m. He was fascinated with his discovery, and realized the importance of the number K for Tibetans. Mashiro knows that, when setting up the antennas, neighboring temples should receive distinct frequencies to avoid interference. Having this constraint in mind and knowing the importance of the number K for the Tibetans, Mashiro wishes to find an assignment of frequencies to antennas that uses at most K distinct frequencies, or determine that this assignment is impossible.

Mashiro had an idea to solve this problem and has already started coding it. Can you beat him to it?

Input

The first line has three integers, N, M, and K, the number of temples, the number of paths that join neighboring temples and the special number revered by the Tibetan people, respectively. The temples are represented by numbers from 1 to N.

The next M lines contain two distinct integers each. Each pair of integers represents two neighboring temples. No two lines among these M lines contain the same pair of integers.

1 ≤ N ≤ 5·104
0 ≤ M ≤ 5·105
1 ≤ K ≤ N
1 ≤ fi ≤ K

Output

If there is no possible frequency assignment to the temples, print a line containing “-1” (without the double quotes). Otherwise, print N lines. The i-th line must contain a single integer, fi, the frequency assigned to temple i, where 1 ≤ fi ≤ K. If there is more than one solution, any one will do.

Examples

input
4 0 1

output

1
1
1
1

input

3 3 3
1 2
2 3
1 3

output

1
2
3

input

3 2 1
1 2
2 3

output

-1

First of all !! 读题!!!!
仔细理解题目中 mK+1 的暗示和意义(一开始以为没有用处)
然后结合图,理解dfs的特性

dfs一遍就好(想想为什么可行,bfs为什么不行,先遇到的分支先搜索为什么不行)

//找思路 + 搜索分析
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e4+10;
int col[maxn];
int tot = 0;
vector<int> vec[maxn];
int n, m, k;

void dfs(int u){
    for(int i = 0; i < vec[u].size(); i++){
        int v = vec[u][i];
        if(col[v] == 0){
            col[v] = (col[u]+1) % k ? (col[u]+1)%k : k;
            dfs(v); 
        } 
    }
}

int main()
{

    scanf("%d%d%d", &n, &m, &k);
    if(k == 1 && m != 0) {
        printf("-1\n"); return 0;
    }

    for(int i = 0; i < m; i++){
        int u, v;
        scanf("%d%d", &u, &v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }

    for(int i = 1; i <= n; i++){
        if(col[i] == 0){
            col[i] = 1;
            dfs(i);
        }
        printf("%d\n", col[i]);
    }
    return 0;
}

Teamwork***

time limit per test2.0 s
memory limit per test512 MB

Teamwork is highly valued in the ACM ICPC. Since the beginning, the ICPC has distinguished itself from other programming contests in that teamwork is a key factor.

The University of Beijing, host of next year’s World Finals, is planning recreational activities for the participants. They want to prepare games that show the importance of teamwork and enable new acquaintances among the contestants from all over the world. One of the staff members has been thinking about the following game.

In a court we have several N-person teams. The goal of each team is roughly as follows: starting from a corner, each team must take all of its members to the other corner of the court. The winning team is the one that finishes this in the shortest time.

Now we explain the game more formally. Consider the team that starts at corner A and must take all of its members to corner B. The following procedure is repeated:

While there are team members at corner A,
· If there is only one team member, he is the only one that goes to corner B.
· Otherwise, two team members must tie one leg of each with a rope and go to B.
· Once arriving at B, they untie their legs, if applicable.
· If there are still team members at corner A, some team member at B must return to A with the rope.

The organization wants to form the teams so that no team has an advantage. They entrusted you with the following task. Given the time in seconds that the members of a team take to go from a corner to the other, find the minimum time it takes for the team to take all its members from corner A to corner B. When two team members cross the court with their legs tied, the time for the pair to cross is the maximum between the times of the two members.

Input

The first line of input contains an integer N, the number of team members. The next line contains N integers ti, the time in seconds that the i-th team member takes to go from one corner to the other.

1 ≤ N ≤ 105
1 ≤ ti ≤ 109

Output

Print a single integer, the minimum time required for the whole team to reach corner B.

Examples

input

3
30 40 50

output

120

input

4
10 20 50 100

output

170

Note

In the first example, the process can be completed in 120 seconds as follows:

The first and second members go from A to B in 40 seconds.
The first member returns to corner A in 30 seconds.
The first and third team members go from A to B in 50 seconds.
This is not the only way to complete the process in this total time, but no other way does it in strictly less than 120 seconds.

理解了样例就很简单, 有点像脑筋急转弯*

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
ll t[maxn];


int main()
{
    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; i++){
        scanf("%d", &t[i]);
    }
    sort(t, t+n);
    ll ans = 0;
    switch(n){
        case 1: ans += t[0]; break;
        case 2: ans += t[1]; break;
        case 3: ans += t[0] + t[1] + t[2];
    }
    if(ans)
        printf("%I64d\n", ans);
    else{
        while(n > 3){
            int h = t[n-1], l = t[n-2];
            ans += min(2*t[1]+t[0]+h, 2*t[0]+h+l); //这里可能会爆Int, WA了一发
            n -= 2;
        }
        switch(n){
            case 1: ans += t[0]; break;
            case 2: ans += t[1]; break;
            case 3: ans += t[0] + t[1] + t[2];
        }
        printf("%I64d\n", ans);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值