2019 Multi-University Training Contest 1

 

Question number A B C D E F G H I J K L M
status . . . THE THE . . . . . . . .

1004 Vacation

Meaning of the questions: given n car distance from the finish, the car length, speed. When the car hit the car in front, the speed will be consistent and in front of the car, find the last time a car through the front end needs.

Ideas: it is plausible that, if the car will hit the car in front, then the last time after the end of the speed of the car in front and only about, and speed of itself irrelevant. So we use a double pointer, pos1 represents the current car, pos2 behalf I want to catch up with the car, calculate all vehicles whether it will encounter in front of the car in front of the car in front stops (we assume that the last car hit the front end instant stop), if you can, then pos1 = pos2, under pos2 find one, if not, then look for a pos2 (the current car will not necessarily affect a car in front of) a final Saowan found speed of the car is the final state, a simple calculation can get the answer.

#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=100010;
struct node{
    double len,dis,v;
}a[maxn];
int n;
double sum[maxn];
bool check(int pos1,int pos2){
    double t2=(a[pos2].dis+sum[pos2]-sum[1])/a[pos2].v;
    double t1=(a[pos1].dis+sum[pos1]-sum[1])/a[pos1].v;
    if(t1>t2)return false;
    return true;
}
int main(){
    while(cin>>n){
        n++;
        for(int i=1;i<=n;i++){
            scanf("%lf",&a[i].len);
            sum[i]=sum[i-1]+a[i].len;
        }
        for(int i=1;i<=n;i++){
            scanf("%lf",&a[i].dis);
        }
        for(int i=1;i<=n;i++){
            scanf("%lf",&a[i].v);
        }
        int pos1=1,pos2=2,car=1;
        while(pos2<=n){
            if(check(pos1,pos2)){
        //        printf("pos1:%d  pos2:%d\n",pos1,pos2);
                car=pos2;
                pos1=pos2,pos2++;
            }else{
                pos2++;
            }
        }
    //    printf("car:%d\n",car);
        printf("%.10f\n",(a[car].dis+sum[car]-sum[1])/a[car].v);
        
    }
}
View Code

 

1005  Path

The meaning of problems: given a directed graph, a plurality of sides blocking request, so that the shortest or longer blocked, to block the cost and side length, minimum cost requirements.

Idea: first of all ran out of the shortest, most belonging to rebuild short-side chart, the original problem is equivalent to finding the minimum cut.

The idea is simple, but ten thousand point I can not write, think about after the game, feeling the shortest side may be many, but certainly will not be the complexity (ie not have a lot of turn ring), so look for network flow the number of augmenting path will not be too much, so it will not time out (forced to explain)

#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=10010;
ll inf=0x3f3f3f3f3f3f3f3f;
struct node {
    int u;
    ll w;
    friend bool operator<(const node &a,const node &b) {
        return a.w>b.w;
    }
};
priority_queue<node >q;
ll dis[2][maxn];
int n,m,u,v,tot[2],head[2][maxn];
ll w;
struct edge {
    int to,Next;
    ll w;
} a[2][maxn<<1];
struct ed{
    int u,v;
    ll w;
}in[maxn];
void init() {
    tot[0]=tot[1]=0;
    for(int i=1; i<=n; i++) {
        dis[0][i]=dis[1][i]=inf;
        head[0][i]=head[1][i]=-1;
    }
}
void addv(int u,int v,ll w,int typ) {
    a[typ][++tot[typ]].to=v;
    a[typ][tot[typ]].w=w;
    a[typ][tot[typ]].Next=head[typ][u];
    head[typ][u]=tot[typ];
}
void dij(int s,int t,int typ){
    dis[typ][s]=0;
    priority_queue<node >q;
    q.push({s,0});
    while(!q.empty()){
        node st=q.top();
        q.pop();
        int u=st.u;
        for(int i=head[typ][u];i!=-1;i=a[typ][i].Next){
            int v=a[typ][i].to;
            if(dis[typ][v]>dis[typ][u]+a[typ][i].w){
                dis[typ][v]=dis[typ][u]+a[typ][i].w;
                q.push({v,dis[typ][v]});
            }
        }
    }
}
struct Edge {
    int to;
    ll  flow;
    int  nxt;
    Edge() {}
    Edge(int to, int nxt, ll flow):to(to),nxt(nxt), flow(flow) {}
} edge[maxn << 2];

int hd[maxn], dep[maxn];
int S, T;
int N, tol;

void Init(int n) {
    N = n;
    for (int i = 0; i <=N; ++i) hd[i] = -1;
    tol = 0;
}

void add(int u, int v, ll w, ll rw = 0) {
    edge[tol] = Edge(v, hd[u], w);
    hd[u] = tol++;
    edge[tol] = Edge(u, hd[v], rw);
    hd[v] = tol++;
}

bool BFS() {
    for (int i = 0; i <= N; ++i) dep[i] = -1;
    queue<int>q;
    q.push(S);
    dep[S] = 1;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = hd[u]; ~i; i = edge[i].nxt) {
            if (edge[i].flow && dep[edge[i].to] == -1) {
                dep[edge[i].to] = dep[u] + 1;
                q.push(edge[i].to);
            }
        }
    }
    return dep[T] < 0 ? 0 : 1;
}

ll DFS(int u, ll f) {
    if (u == T || f == 0) return f;
    ll w, used = 0;
    for (int i = hd[u]; ~i; i = edge[i].nxt) {
        if (edge[i].flow && dep[edge[i].to] == dep[u] + 1) {
            w = DFS(edge[i].to, min(f - used, edge[i].flow));
            edge[i].flow -= w;
            edge[i ^ 1].flow += w;
            used += w;
            if (used == f) return f;
        }
    }
    if (!used) dep[u] = -1;
    return used;
}

ll Dicnic() {
    ll ans = 0;
    while (BFS()) {
        ans += DFS(S, inf);
    }
    return ans;
}

int main(){
    int TT;
    cin>>TT;
    while(TT--){
        cin>>n>>m;
        init();
        for(int i=1;i<=m;i++){
            scanf("%d%d%lld",&in[i].u,&in[i].v,&in[i].w);
            addv(in[i].u,in[i].v,in[i].w,0);
            addv(in[i].v,in[i].u,in[i].w,1);
        }
        dij(1,n,0);
        dij(n,1,1);
        tot[0]=0;
        for(int i=1; i<=n; i++) {
            head[0][i]=-1;
        }
        S=1,T=n;
        Init(n);
        for(int i=1;i<=m;i++){
            int u=in[i].u,v=in[i].v;
            ll w=in[i].w;
            if(dis[0][u]+w+dis[1][v]==dis[0][n]){
                add(u,v,w);
            }
        }
        printf("%lld\n",Dicnic());
    //    printf("debug\n");
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/mountaink/p/11231552.html