POJ 1511 SPFA 模板

来源:互联网 发布:我国历年gdp数据 编辑:程序博客网 时间:2024/05/17 22:09
/*--------------------- #headfile--------------------*/#include <algorithm>#include <iostream>#include <sstream>#include <cstring>#include <cstdlib>#include <cassert>#include <cstdio>#include <vector>#include <cmath>#include <queue>#include <stack>#include <set>#include <map>/*----------------------#define----------------------*/#define DRII(X,Y) int (X),(Y);scanf("%d%d",&(X),&(Y))#define EXP 2.7182818284590452353602874713527#define CASET int _;cin>>_;while(_--)#define RII(X, Y) scanf("%d%d",&(X),&(Y))#define DRI(X) int (X);scanf("%d", &X)#define mem(a,b) memset(a,b,sizeof(a))#define rep(i,n) for(int i=0;i<n;i++)#define ALL(X) (X).begin(),(X).end()#define INFL 0x3f3f3f3f3f3f3f3fLL#define RI(X) scanf("%d",&(X))#define SZ(X) ((int)X.size())#define PDI pair<double,int>#define rson o<<1|1,m+1,r#define PII pair<int,int>#define MAX 0x3f3f3f3f#define lson o<<1,l,m#define MP make_pair#define PB push_back#define SE second#define FI firsttypedef __int64 ll;template<class T>T MUL(T x,T y,T P){T F1=0;while(y){if(y&1){F1+=x;if(F1<0||F1>=P)F1-=P;}x<<=1;if(x<0||x>=P)x-=P;y>>=1;}return F1;}template<class T>T POW(T x,T y,T P){T F1=1;x%=P;while(y){if(y&1)F1=MUL(F1,x,P);x=MUL(x,x,P);y>>=1;}return F1;}template<class T>T gcd(T x,T y){if(y==0)return x;T z;while(z=x%y)x=y,y=z;return y;}#define DRIII(X,Y,Z) int (X),(Y),(Z);scanf("%d%d%d",&(X),&(Y),&(Z))#define RIII(X,Y,Z) scanf("%d%d%d",&(X),&(Y),&(Z))const double pi = acos(-1.0);const double eps = 1e-6;const ll mod = 1000000007ll;const int M = 100005;const int N = 1000005;using namespace std;/*----------------------Main-------------------------*/struct Edge {    int to;    ll w;    Edge() {}    Edge(int _to, ll _w) {        to = _to, w = _w;    }};int n, m;int x[N], y[N], z[N];ll d[N], f[N];vector<Edge> G[N];bool vis[N];int cnt[N];bool SPFA(int st, ll *d) {    for(int i = 0; i < n; i++) d[i] = 1e18;    d[st] = 0;    queue<int> Q;    Q.push(st);    mem(vis, 0);    mem(cnt, 0);    vis[st] = 1;    while(!Q.empty()) {        int u = Q.front(); Q.pop();        vis[u] = 0;        for(int i = 0; i < SZ(G[u]); i++) {            Edge e = G[u][i];            if(d[e.to] > d[u] + e.w) {                d[e.to] = d[u] + e.w;                if(!vis[e.to]) {                    Q.push(e.to);                    vis[e.to] = 1;                    if(++cnt[e.to] > n) return 0;                }            }        }    }    return 1;}void solve() {    RII(n, m);    for(int i = 0; i < n; i++) G[i].clear();    for(int i = 0; i < m; i++) {        RIII(x[i], y[i], z[i]);        x[i]--, y[i]--;        G[x[i]].PB(Edge(y[i], z[i]));    }    SPFA(0, d);    for(int i = 0; i < n; i++) G[i].clear();    for(int i = 0; i < m; i++) {        G[y[i]].PB(Edge(x[i], z[i]));    }    SPFA(0, f);    ll ans = 0;    for(int i = 0; i < n; i++) {        ans += f[i] + d[i];    }    printf("%I64d\n", ans);}int main() {//    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);    CASET    solve();    return 0;}

0 0
原创粉丝点击