标签:include \n strong code 博客 bool AC .cpp com
推荐博客:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html
-----------------------------------------------------------
http://poj.org/problem?id=3169
1 #include "iostream" 2 #include <string.h> 3 #include <stdio.h> 4 using namespace std; 5 6 #define INF 1e9 7 const int maxn = 10009*2; 8 int n, ml, md, index, m; 9 int head[maxn], to[maxn], pre[maxn], w[maxn], queue[maxn], dis[maxn], cnt[maxn]; 10 11 void init() { 12 index = 1; 13 for(int i = 1; i <= n; ++i) head[i] = 0, cnt[i] = 0; 14 } 15 16 void add(int x, int y, int z) { 17 to[index] = y; 18 w[index] = z; 19 pre[index] = head[x]; 20 head[x] = index++; 21 } 22 23 bool mark[maxn]; 24 25 int spfa() { 26 int front = 0, rear = 0; 27 for(int i = 1; i <= n; ++i) { 28 dis[i] = INF; 29 add(i, i-1, 0); 30 } 31 dis[1] = 0; 32 queue[rear++] = 1; 33 while(front != rear) { 34 int k = queue[front++]; 35 mark[k] = false; 36 for(int i = head[k]; i; i = pre[i]){ 37 if(dis[k] + w[i] < dis[to[i]]) { 38 dis[to[i]] = dis[k] + w[i]; 39 if(!mark[to[i]]) { 40 if(++cnt[to[i]] > n) return -1; 41 queue[rear++] = to[i]; 42 mark[to[i]] = true; 43 } 44 } 45 } 46 } 47 if(dis[n] == INF) return -2; 48 return dis[n]; 49 } 50 51 int main() { 52 #ifdef wenbao 53 freopen("in", "r", stdin); 54 #endif 55 while(~scanf("%d%d%d", &n, &ml, &md)) { 56 init(); 57 int x, y, z; 58 for(int i = 0; i < ml; ++i) { 59 scanf("%d%d%d", &x, &y, &z); 60 add(x, y, z); 61 } 62 for(int i = 0; i < md; ++i) { 63 scanf("%d%d%d", &x, &y, &z); 64 add(y, x, -z); 65 } 66 printf("%d\n", spfa()); 67 } 68 return 0; 69 }
只有不断学习才能进步!
标签:include \n strong code 博客 bool AC .cpp com
原文地址:https://www.cnblogs.com/wenbao/p/7646169.html