6 test T2 solution to a problem country pig (pig) - Yali 19 - 8

T2 pig States

Description title destination time

Pigs are ⼀ a country by the \ (n \) countries, cities composed.
The king realized that "to get rich, first build roads" The importance of this sentence, which determines the size zoomed road. Unfortunately, the country's pig
pigs do not usually ⼯ process, so they can only build your chicken chicken spree next country to help build roads. Chicken construction spree despise pig, so with
it the construction of a \ (m \) unidirectional way. Nevertheless, every road or after- production value more or less.
Road was built, the economy was able to come up. The king through research, found a giant zoomed defective roads. Specifically, the direction that the pig ⺠ are
not good sense, if there is, dry road can be composed of ⼀ ring, then the poor pig ⺠ it is possible to go around the ring ⾥ ⾯, which ⼀ once
like even ⾄ become extremely opposite effect.
King believes this is not good. It determines the path to improve such that the composition can no longer loop path, so it is good.
King found the chicken was built spree after-sales service requirements, but the chicken was built spree only promised to reverse if, dry road, but also charge a fee required
to apply it. King ⽢ Center Weighted not be any mercy, through negotiations, for the total cost Use value is the reverse of the road from the largest value.
Chicken built spree to ensure there are ways to make the road becomes good. The king wanted to know the best way to become a good ⼩ fee Use. Of course, if
some way have been very good, then explained the king's research problems, natural coloring fee Use is 0.

START input format

First frame ⾏ two integers \ (n, m \) respectively denote the number of destination time monitor the number of destination time and city road.
Next ⾏, three integers each ⾏ \ (x, y, z \ ) table is displayed ⼀ this article from the X-$ \ (one-way to the city \) the y- \ (road of the city, its value is \) z $ .

Output Format

⼀ ⾏ ⼀ integer table displayed answer.

Sample

Input 1
5 6
2 1 1
5 2 6
2 3 2
3 4 3
4 5 5
1 5 4
Output 1
2

Input 2
5
7
2 1 5
3 2 3
1 3 3
2 4 1
4 3 5
5 4 1
1 5 3
Output 2
3

Data range
for 30% of data, n, m <= 20.
For 60% of the data, n, m <= 100.
To 100% of the data, 2 <= n, m < 1e5,1 <= x, y <= n, 1 <= z <= 1e9.
Graded data.

First of all, we see that people are out of question(Mo)Have (Ming)Road (Qi) Reason(Miao)The solution to a problem.

⼆ minute answer. All right edge of the current side ⼆ zoomed out scores ⼀ build a map.
There is not ⾏ ring, or can (even zoomed ⼩ both sides from the topology to the topology of small dots sequence order, ring ⼀ not given).

But in fact, the idea is this blogger.(Virtually identical)

Dichotomous answer a value (data) (note, here is the data "MAX value is the value of the reverse of the road"), while the right side is larger than the data can not move (the right side is smaller than the data you are free to fiddle) . Ever since these can not be made a movable side in FIG. Okay, drawing was finished, the topology that, if Diao out a ring, that is clearly inconsistent with the requirements of pig king, then it would have to rise data, the card off some edges in the ring. (Because you want this figure to become a DAG, you at least have to put this figure to destroy). Yo, up more, had to drop. Such slow-half plane Data plane out, it must be the most appropriate data. Doing so, the time complexity is O (n log n), no problem

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int maxm = 1e5 + 5;
// n , m 的范围。
struct edge{int to, z;};
vector <edge> e[maxn];
// vector 存图
struct group{int u,v,w;}g[maxm];
// 用struct保存每一条边,以便二分时不断建图。
int n, m, x, y, z, cnt, ans;
//ans 是 最终的 data
void init(){
    for(int i = 1;i <= n;i ++){
        e[i].clear();
    }
}
//每次建图之前需要清零
bool work(int limit){
    //拓扑排序
    queue <int> q;
    int rd[maxn] = {0}, dot = 0;
    //dot是拓扑确定顺序的点数的数量
    init();
    for(int i = 1;i <= m;i ++){
        if(g[i].w > limit){
            e[g[i].u].push_back((edge){g[i].v,g[i].w}); 
            rd[g[i].v] ++;
        }
    }
    //建图
    for(int i = 1;i <= n;i ++){
        if(rd[i] == 0){
            dot ++;
            q.push(i);
        }
    }
    while(!q.empty()){
        int x = q.front();
        q.pop();
        for(int i = 0;i < e[x].size(); i ++){
            int y = e[x][i].to;
            rd[y] --;
            if(rd[y] == 0){
                q.push(y);
                dot ++;
            }
        }
    }
    //标准的topsort过程
    return dot == n;
    //如果图中有环,那么有些点显然是无法确定顺序的,自然dot != n
}
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1;i <= m;i ++){
        scanf("%d%d%d", &x, &y, &z);    
        g[++ cnt] = (group){x,y,z};
        //记录边数
    }
    int l = 0, r = 1e9;
    //二分data
    while(l <= r){
        int mid = l + r >> 1;//当前data
        if(!work(mid)){//如果当前data都爆出了环,那更小的data您就别想了
            l = mid + 1;
        } else {
            //如果可以的话,那就再更苛刻的范围内求data
            r = mid - 1;
            ans = mid;
        }
    }
    cout << ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/yangxuejian/p/11318598.html
pig
pig