Wormholes 虫洞 BZOJ 1715 spfa判断负环
John在他的農(nóng)場(chǎng)中閑逛時(shí)發(fā)現(xiàn)了許多蟲洞。蟲洞可以看作一條十分奇特的有向邊,并可以使你返回到過(guò)去的一個(gè)時(shí)刻(相對(duì)你進(jìn)入蟲洞之前)。John的每個(gè)農(nóng)場(chǎng)有M條小路(無(wú)向邊)連接著N (從1..N標(biāo)號(hào))塊地,并有W個(gè)蟲洞。其中1<=N<=500,1<=M<=2500,1<=W<=200。 現(xiàn)在John想借助這些蟲洞來(lái)回到過(guò)去(出發(fā)時(shí)刻之前),請(qǐng)你告訴他能辦到嗎。 John將向你提供F(1<=F<=5)個(gè)農(nóng)場(chǎng)的地圖。沒(méi)有小路會(huì)耗費(fèi)你超過(guò)10000秒的時(shí)間,當(dāng)然也沒(méi)有蟲洞回幫你回到超過(guò)10000秒以前。
NO YESSample Output
Input
* Line 1: 一個(gè)整數(shù) F, 表示農(nóng)場(chǎng)個(gè)數(shù)。
* Line 1 of each farm: 三個(gè)整數(shù) N, M, W。
* Lines 2..M+1 of each farm: 三個(gè)數(shù)(S, E, T)。表示在標(biāo)號(hào)為S的地與標(biāo)號(hào)為E的地中間有一條用時(shí)T秒的小路。
* Lines M+2..M+W+1 of each farm: 三個(gè)數(shù)(S, E, T)。表示在標(biāo)號(hào)為S的地與標(biāo)號(hào)為E的地中間有一條可以使John到達(dá)T秒前的蟲洞。
Output* Lines 1..F: 如果John能在這個(gè)農(nóng)場(chǎng)實(shí)現(xiàn)他的目標(biāo),輸出"YES",否則輸出"NO"。
Sample Input2 3 3 1 1 2 2 1 3 4 2 3 1 3 1 3 3 2 1 1 2 3 2 3 4 3 1 8 #include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 200005 #define inf 0x7fffffff //#define INF 1e18 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-5 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii;inline int rd() {int x = 0;char c = getchar();bool f = false;while (!isdigit(c)) {if (c == '-') f = true;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f ? -x : x; }ll gcd(ll a, ll b) {return b == 0 ? a : gcd(b, a%b); } int sqr(int x) { return x * x; }/*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) {if (!b) {x = 1; y = 0; return a;}ans = exgcd(b, a%b, x, y);ll t = x; x = y; y = t - a / b * y;return ans; } */ int T; int n, m, W; struct node {int u, v, w, nxt; }e[maxn]; int head[maxn], tot; int dis[maxn]; void addedge(int u, int v, int w) {e[++tot].u = u; e[tot].v = v; e[tot].w = w;e[tot].nxt = head[u]; head[u] = tot; } int num[maxn]; bool vis[maxn];bool spfa() {queue<int>q;q.push(1); vis[1] = 1; dis[1] = 0;num[1] = 1;while (!q.empty()) {int u = q.front(); q.pop();vis[u] = 0;for (int i = head[u]; i; i = e[i].nxt) {int v = e[i].v; int w = e[i].w;if (dis[v] > dis[u] + w) {dis[v] = dis[u] + w;if (!vis[v]) {q.push(v); vis[v] = 1; num[v]++;if (num[v] > n)return false;}}}}return true; }int main() {//ios::sync_with_stdio(0);T = rd();while (T--) {n = rd(); m = rd(); W = rd();for (int i = 1; i <= n; i++)dis[i] = inf;ms(head); tot = 0; ms(vis); ms(e); ms(num);for (int i = 1; i <= m; i++) {int u, v, w; u = rd(); v = rd(); w = rd();addedge(u, v, w); addedge(v, u, w);}for (int i = 1; i <= W; i++) {int u, v, t;u = rd(); v = rd(); t = rd();addedge(u, v, -t);}if (spfa()) {cout << "NO" << endl;}else cout << "YES" << endl;}return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/zxyqzy/p/10351060.html
總結(jié)
以上是生活随笔為你收集整理的Wormholes 虫洞 BZOJ 1715 spfa判断负环的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: fzyzojP3372 -- [校内训练
- 下一篇: Java输出数组中最长递增子序列的代码