CSU1978: LXX的圖論題
Description
由於lxx的圖論和數據結構太弱了,大佬Z決定爲lxx補一補。於是大佬Z爲lxx出了一道題目,題目如下:給出一張有向圖,圖中有n個點,m條邊,每條邊上都有一個權值w,問圖中是否存在滿足以下條件的點i,j,...p使得不等式w[i][j] * w[j][k] * .... * w[p][i]<1成立。奈何lxx太弱了,他決定尋求你的幫助。
Input
多組輸入,以文件結尾。第一行兩個整數n( 1<=n<=500 ),m( 1<=m<=n*(n-1)/2 ),接下來m行,每行3個數x,y,z,(x≠y):表示x到y有一條邊,權值爲z(0<z<20,且保證z小數點後面最多隻有一位)。
Output
如果存在滿足題目所描述的式子,輸出“YES”,否則輸出“NO”。
Sample Input
2 2
1 2 0.9
2 1 1.2
6 4
1 2 0.1
2 4 0.8
4 1 12
4 1 15
Sample Output
NO
YES
Hint
點的編號爲1~n
Source
2017年8月月賽
Author
廖璇璇
題目就叫圖論題,加上題中給出了“w[i][j] * w[j][k] * …. * w[p][i]<1”的約束條件,容易想到最短路算法,並且這個最短路是帶有負環的,加上頂點數量不多,所以可以循環地調用遞歸的SPFA來檢查每一個頂點。若存在從某個頂點出發又回到出發點的負環,並且路徑權值之積小於1,就可以中斷並輸出結果了。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 504, INF = 0x7fffffff;
int n, m, cnt, start;
bool flag;
int u[maxn*maxn], v[maxn*maxn], first[maxn*maxn], Next[maxn*maxn]; // 注意預留足夠的空間存儲邊。
double w[maxn*maxn], d[maxn]; // 注意權值爲浮點型。
bool vis[maxn];
void init(){
memset(first, -1, sizeof(first));
memset(Next, -1, sizeof(Next));
for(int i = 0; i < maxn; i++)
d[i] = INF;
cnt = 0;
flag = false;
}
void makeEdge(int a, int b, double x){
u[cnt] = a;
v[cnt] = b;
w[cnt] = x;
Next[cnt] = first[a];
first[a] = cnt++;
}
void spfa(int now){
if(flag) return;
vis[now] = true;
for(int i = first[now]; i != -1; i = Next[i]){
if(flag) return;
int to = v[i];
if(d[to] > d[now]*w[i]){
d[to] = d[now]*w[i];
if(to == start && d[to]<1){ // 存在符合條件的負環即中斷。
flag = true;
return;
}
else
spfa(to);
}
}
vis[now] = false;
}
int main(){
#ifdef TEST
freopen("test.txt", "r", stdin);
#endif // TEST
while(cin >> n >> m){
init();
int a, b; double c;
for(int i = 0; i < m; i++){
scanf("%d%d%lf", &a, &b, &c);
makeEdge(a, b, c);
}
for(int i = 1; i <= n; i++){
start = i;
memset(vis, false, sizeof(vis));
for(int j = 0; j <= n; j++)
d[j] = INF;
d[i] = 1; // 到自己的權值賦1,準備與其他權值相乘。
spfa(i);
if(flag) break;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
/**********************************************************************
Problem: 1978
User: xyJiang
Language: C++
Result: AC
Time:120 ms
Memory:25168 kb
**********************************************************************/