1 solutions
-
0
C++ :
/* author :hzoi_ztx title :最小圈 ALG :01分数规划 SPFA判负环 comment : 2014 10 08 test */ #include <cstdio> #include <cstring> #define maxn 3010 #define maxm 10010 #define info 4500.0 #define accu 0.000000001 struct FST{ int to , next ; double len ; } map[maxm] ; int star[maxn] = {0} , tot = 0 ; void addEdge(const int u , const int v , const double w) { tot ++ ; map[tot].to = v ; map[tot].len = w ; map[tot].next = star[u] ; star[u] = tot ; } int n , m ; int u , v , p ; double L = -info , R = info , ans , tmp ; bool exist[maxn] ; int len[maxn] ;//记录最短路的长度,当其大于n时存在负环 double dis[maxn] ; int line[maxn+10] , head = 1 , tail = 0 ; inline int F(const int x) { return ((x-1)%n)+1 ; } inline int front() { return line[F(head)] ; } inline int back() { return line[F(tail)] ; } inline void push_head(const int x) { line[F(--head)] = x ; } inline void push_back(const int x) { line[F(++tail)] = x ; } inline void pop_head() { head ++ ; } inline bool empty() { return head>tail ; } bool spfa(double ans) { memset(exist , 1 , sizeof (exist)) ; memset(len , 0 , sizeof (len)) ; memset(dis , 0 , sizeof (dis)) ; for (u = 1 ; u <= n ; u ++ ) push_back(u) ; while (!empty()) { u = front() ; pop_head() ; exist[u] = false ; p = star[u] ; while (p) { v = map[p].to ; tmp = map[p].len-ans ; if (dis[u]+tmp < dis[v]) { dis[v] = dis[u]+tmp ; len[v] = len[u] +1 ; if (len[v] > n) return true ; else if (!exist[v]) { exist[v] = true ; if (dis[v] < dis[front()]) push_head(v) ; else push_back(v) ; } } p = map[p].next ; } } return false ; } void Bsearch() { while (R-L >= accu) { ans = L+(R-L)/2 ; if (spfa(ans)) R = ans ; else L = ans ; } } int main() { //#define READ #ifdef READ freopen("cycle.in" ,"r",stdin ) ; freopen("cycle.out","w",stdout) ; #endif scanf("%d%d", &n , &m ) ; for (int i = 1 ; i <= m ; i ++ ) { scanf("%d%d%lf", &u , &v , &tmp) ; addEdge(u , v , tmp) ; } Bsearch() ; printf("%.8lf\n", ans ) ; return 0 ; }
- 1
Information
- ID
- 715
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By