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
- POJ 1511 SPFA 模板
- poj 1511 spfa 模板题
- POJ-1511 初探Bellman-Ford,再水SPFA模板题..
- poj 1511 Invitation Cards 最短路SPFA(模板题)
- poj 3259 Wormholes(spfa) (spfa模板)
- poj-1125-Stockbroker Grapevine spfa模板
- POJ 3169(差分约束 + spfa模板)
- poj 1511 spfa
- poj 1511 spfa
- poj 1511----spfa
- POJ 1511 spfa
- poj 1511 SPFA
- poj 1511 spfa入门
- poj 1511 SPFA
- poj 1511 spfa水题
- HDU 1535 && POJ 1511 Invitation Cards (SPFA 模板 + 反向建图)
- spfa模板
- spfa 模板
- leetcode_Median of Two Sorted Arrays
- LeetCode(116) Populating Next Right Pointers in Each Node
- 智慧城市
- UE4 移动平台游戏开发
- iOS 9之分屏多任务(Split View)
- POJ 1511 SPFA 模板
- C++对象模型——对象成员的效率 (Object Member Efficiency)(第三章)
- cocos2d-x 2.0版本 自适应屏幕分辨率
- poj 1035 Spell checker
- 深度学习(四)卷积神经网络入门学习(1)
- 动画延迟消失
- 树形DP 没有上司的舞会
- UE4 iOS游戏开发
- 《unix环境高级编程》 有关I/O库函数--3