uva 1161                图论       无从下手。。。

uva 10938              LCA && RMQ。。

uva 10246              应该可以用变形floyd预处理,然后O(1)回答询问。WA代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>

using namespace std;

#define LL long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define cint const int

#define MAXN 100
#define INF 1000000000

int n, m, f[MAXN];
int maxcst[MAXN][MAXN], w[MAXN][MAXN];
LL d[MAXN][MAXN];

void floyd(){
    int i, j, k;

    for(i=1; i<=n; i++)
        for(j=1; j<=n; j++){
            maxcst[i][j]=maxcst[j][i]=max(f[i], f[j]);
            d[i][j]=d[j][i]=w[i][j]+maxcst[i][j];
        }

    for(k=1; k<=n; k++)
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++){
                int tmc = max(maxcst[i][k], maxcst[k][j]);
                int t = d[i][k] + d[k][j] - min(maxcst[i][k], maxcst[k][j]);
                if(t < d[i][j]){
                    d[i][j]=t;
                    maxcst[i][j]=tmc;
                }
            }
}

int main(){
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
    int q, kase=1;
    while(scanf(" %d %d %d", &n, &m, &q)==3 && (n || m || q)){
        int i, u, v, tw;
        for(i=1; i<=n; i++){
            scanf(" %d", f+i);
            fill_n(w[i]+1, n, INF);
        }
        for(i=1; i<=n; i++) w[i][i]=0;
        for(i=0; i<m; i++){
            scanf(" %d %d %d", &u, &v, &tw);
            if(u!=v) w[u][v]=w[v][u]=min(w[u][v], tw);
        }
        floyd();
        if(kase>1) printf("\n");
        printf("Case #%d\n", kase++);
        while(q--){
            scanf(" %d %d", &u, &v);
            if(d[u][v]>=INF) printf("-1\n");
            else printf("%lld\n", d[u][v]);
        }
    }
    return 0;
}

 uva 1357 交了一发submission error的递归dfs。。。非递归的还不知道写。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>

using namespace std;

#define LL long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define cint const int

#define MAXN 20000002
#define MAXM MAXN
#pragma comment(linker,"/STACK:102400000,102400000")

struct edge{
    int u, v, nxt;
}e[MAXM];
int h[MAXN], cc, n, tsp;

int pre[MAXN], post[MAXN];
void dfs(int u){
    pre[u] = tsp++;
    for(int i=h[u]; i!=-1; i=e[i].nxt)
        dfs(e[i].v);
    post[u] = tsp++;
}
        //unfinished
void sim_dfs(int u){
    stack<int> s;
    s.push(u);
    while(!s.empty()){
        u = s.top();
        pre[u] = tsp++;
        for(int i=h[u]; i!=-1; i=e[i].nxt)
            s.push(e[i].v);
        post
    }
}

int main(){
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
    int T, kase=1;
    scanf(" %d", &T);
    while(T--){
        int i, u, c, cnt=1;
        scanf(" %d", &n);
        fill_n(h, n, -1);   cc=0;
        for(i=0; i<n; i++){
            scanf(" %d", &c);
            while(c--){
                e[cc]=(edge){i, cnt++, h[i]};
                h[i]=cc++;
            }
        }
        if(cnt>n) fill(h+n, h+cnt, -1);
        tsp = 0;
        dfs(0);
        scanf(" %d", &c);
        if(kase>1) printf("\n");
        printf("Case %d:\n", kase++);
        while(c--){
            int v;
            scanf(" %d %d", &u, &v);
            if(pre[u]<pre[v] && post[u]>post[v]) printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}
posted @ 2013-10-10 10:57  Ramanujan  阅读(164)  评论(0编辑  收藏  举报