处理负权边(最短路)

const int maxn=100005;
const int maxm=200005;
int T,n,m;
int tot,head[maxn],rd[maxn];
ll dis[maxn];
int u[maxm],v[maxm],c[maxm],a[maxm],b[maxm];
ll ans;
 
struct {
    int v;
    ll cost;
    int next;
}e[maxm];
 
queue<int> que;
 
void init() {
    tot=0;
    memset(rd,0,sizeof rd);
    memset(head,-1,sizeof head);
    memset(dis,0x3f,sizeof dis);
    while(!que.empty())
        que.pop();
}
 
void addedge(int u,int v,ll cost) {
    e[tot]={v,cost,head[u]};
    head[u]=tot++;
    rd[v]++;
}
 
ll solve()
{
    dis[1]=0;
    for(int i=1;i<=n;i++)
        if(rd[i]==0)
            que.push(i);
    while(!que.empty()) {
        int u=que.front();
        que.pop();
        for(int i=head[u];i!=-1;i=e[i].next) {
            int v=e[i].v;
            ll cost=e[i].cost;
            if(dis[v]>dis[u]+cost)
                dis[v]=dis[u]+cost;
            rd[v]--;
            if(rd[v]==0)
                que.push(v);
        }
    }
    return dis[n];
}
 

 

posted @ 2019-01-26 22:31  better46  阅读(177)  评论(0编辑  收藏  举报