1. 程式人生 > >hdu4966 最小樹形圖+虛根

hdu4966 最小樹形圖+虛根

ace else 什麽 string ems oot set 增加 printf

/*

辛辛苦苦調試半天,

過了樣例,竟然沒有ac!!

網上對比了ac代碼,感覺添加一個虛根就能ac

但是想不明白為什麽

*/

我的代碼

/*
hdu4966
第i門課從0到a[i]個等級,從最開始所有課都是0級,目標所有課到最高級
M條路,如果第i門課達到L1[i],那麽通過M後,第j門課到達L2[i]
N,M
a[i] .. a[N]
接下來M行c[i], L1[i], d[i], L2[i], money[i]
邊權當然是money,每門課拆成a[i]個等級(500個點)
每門課的a[i]到a[i]-1連一條權值為0的邊 
增加一個超級源點,和每門課level 0連在一起 
*/
#include<iostream>
#include
<cstdio> #include<cstring> #define MAXN 505 #define INF 0x3f3f3f using namespace std; struct Edge{ int u,v,cost; }edge[MAXN*MAXN]; int a[MAXN];//等級 int V[MAXN][MAXN];//點集 int c[MAXN],L1[MAXN],d[MAXN],L2[MAXN],money[MAXN]; int pre[MAXN],id[MAXN],vis[MAXN],in[MAXN]; int zhuliu(int root,int
n,int m){ int res=0; int u,v; while(1){ for(int i=0;i<n;i++) in[i]=INF; for(int i=0;i<m;i++) if(edge[i].u!=edge[i].v && edge[i].cost<in[edge[i].v]){ pre[edge[i].v]=edge[i].u; in[edge[i].v]=edge[i].cost; }
for(int i=0;i<n;i++) if(i!=root && in[i]==INF) return -1; int tn=0; memset(id,-1,sizeof id); memset(vis,-1,sizeof vis); in[root]=0; for(int i=0;i<n;i++){ res+=in[i]; v=i; while(v!=root && id[v]==-1 && vis[v]!=i){ vis[v]=i; v=pre[v]; } if(id[v]==-1 && v!=root){ for(int u=pre[v];u!=v;u=pre[u]) id[u]=tn; id[v]=tn++; } } if(tn==0) break; for(int i=0;i<n;i++) if(id[i]==-1) id[i]=tn++; for(int i=0;i<m;i++){//第四步,更新一次邊集 int u=edge[i].u; int v=edge[i].v; edge[i].u=id[u]; edge[i].v=id[v]; if(id[u] != id[v]) edge[i].cost-=in[v]; } n=tn; root=id[root]; } return res; } int totm; void addedge(int u,int v,int w){ edge[totm].u=u; edge[totm].v=v; edge[totm].cost=w; totm++; } int main(){ int n,m,totn; while(scanf("%d%d",&n,&m) && n && m){ totn=totm=0; memset(V,0,sizeof V); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); for(int j=0;j<=a[i];j++){ V[i][j]=totn++; if(j!=0) addedge(V[i][j],V[i][j-1],0); } } int sum=0,root=0; for(int i=1;i<=m;i++){ scanf("%d%d%d%d%d",&c[i],&L1[i],&d[i],&L2[i],&money[i]); if(L1[i]==0) root=c[i]; addedge(V[c[i]][L1[i]],V[d[i]][L2[i]],money[i]); sum+=money[i]; } if(root==0){ printf("-1\n"); continue; } int ans=zhuliu(root,totn,totm); printf("%d\n",ans); } return 0; }

ac代碼

#include <cstdio>
#include <cstring>
#include <iostream>
 
using namespace std;
 
const int maxn = 555, maxm = 500 * 2000, inf = 1e9;
 
struct Edge{
    int u, v, w;
    Edge(){}
    Edge(int u, int v, int w):
    u(u), v(v), w(w){}
}e[maxm];
int ec;
int lev[55], begid[55], endid[55];
int N;
 
int mincost[maxn], pre[maxn];
bool makeMin(int root){
    for(int i = 1; i <= N; i++) mincost[i] = inf;
    for(int i = 0; i < ec; i++){
        if(e[i].u == e[i].v) continue;
        int v = e[i].v;
        if(mincost[v] > e[i].w){
            mincost[v] = e[i].w;
            pre[v] = e[i].u;
        }    
    }
    for(int i = 1; i <= N; i++){
        if(i == root) continue;
        if(mincost[i] == inf) return false;
    }
    return true;
}
 
int vis[maxn];
int id[maxn];
bool existCircle(int root, int &res){
    memset(vis, -1, sizeof(vis));
    memset(id, -1, sizeof(id));
    int ind = 1;
    mincost[root] = 0;
    for(int i = 1; i <= N; i++){
        int v = i;
        res += mincost[i];
        while(vis[v] != i && v != root && id[v] == -1) {
            vis[v] = i, v = pre[v];
        }
        if(v != root && id[v] == -1){
            for(int u = pre[v]; u != v; u = pre[u]) id[u] = ind;
            id[v] = ind++;
        }
    }
    if(ind == 1) return false;
    for(int i = 1; i <= N; i++)
        if(id[i] == -1) id[i] = ind++;
    N = ind - 1;
    return true;
}
 
void update(){
    for(int i = 0; i < ec; i++){
        int v = e[i].v;
        e[i].u = id[e[i].u];
        e[i].v = id[e[i].v];
        if(e[i].u == e[i].v) continue;
        e[i].w -= mincost[v];            
    }
}
 
int DMST(int root){
    int res = 0;
    while(1){
        if(!makeMin(root)) return -1;
        if(!existCircle(root, res)) return res;
        update();
        root = id[root];
    }
    return -1;
}
 
int main(){
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, m;
    while(scanf("%d%d", &n, &m), n&&m){
        for(int i = 1; i <= n; i++) scanf("%d", lev+i);    
        lev[0] = 1, begid[0] = 0;    
        for(int i = 1; i <= n; i++) begid[i] = begid[i-1] + lev[i-1] + 1;
        N = begid[n] + lev[n];        
        int c, l, d, r, w;
        int root = 1;
        ec = 0;
        for(int i = 1; i <= n; i++)
        e[ec++] = Edge(root, begid[i], 0);
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= lev[i]; j++){
                int u = begid[i] + j, v = u - 1;
                e[ec++] = Edge(u, v, 0);
            }
        }
        for(int i = 0; i < m; i++){
            scanf("%d%d%d%d%d", &c, &l, &d, &r, &w);
            int v = begid[d] + r;
            for(int j = l, u = begid[c] + l; j <= lev[c]; u++, j++){
                e[ec++] = Edge(u, v, w);    
            }
        }    
        int ans = DMST(root);
        if(~ans) printf("%d\n", ans);
        else puts("-1");
    }
    return 0;
}

hdu4966 最小樹形圖+虛根