标签:turn between term cer i+1 number lang iss location
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14484 | Accepted: 6961 |
Description
Input
Output
Sample Input
4 2 1 1 3 10 2 4 20 2 3 3
Sample Output
27
Hint
1 #include <cstdio> 2 #include <queue> 3 #include <cctype> 4 #define num s-‘0‘ 5 using namespace std; 6 7 struct edge 8 { 9 int to; 10 int cost; 11 }; 12 13 const int INF=0x3f3f3f3f; 14 const int MAX_V=1000; 15 vector<edge> G[MAX_V]; 16 int V, ML, MD; 17 int d[MAX_V]; 18 bool inque[MAX_V]; 19 int n[MAX_V]; 20 21 void read(int &x){ 22 char s; 23 x=0; 24 bool flag=0; 25 while(!isdigit(s=getchar())) 26 (s==‘-‘)&&(flag=true); 27 for(x=num;isdigit(s=getchar());x=x*10+num); 28 (flag)&&(x=-x); 29 } 30 31 void write(int x) 32 { 33 if(x<0) 34 { 35 putchar(‘-‘); 36 x=-x; 37 } 38 if(x>9) 39 write(x/10); 40 putchar(x%10+‘0‘); 41 } 42 43 bool spfa(); 44 45 int main() 46 { 47 read(V); read(ML); read(MD); 48 for (int i=0; i<ML; i++) 49 { 50 int A, B, C; 51 read(A); read(B); read(C); 52 edge e; 53 e.to=B-1;e.cost=C; 54 G[A-1].push_back(e); 55 } 56 for (int i=0; i<MD; i++) 57 { 58 int A, B, C; 59 read(A); read(B); read(C); 60 edge e; 61 e.to=A-1;e.cost=-C; 62 G[B-1].push_back(e); 63 } 64 for (int i=0; i<V-1; i++) 65 { 66 edge e; 67 e.to=i;e.cost=0; 68 G[i+1].push_back(e); 69 } 70 if (spfa()) 71 { 72 if (d[V-1]==INF) write(-2); 73 else write(d[V-1]); 74 } 75 else write(-1); 76 } 77 78 bool spfa() 79 { 80 fill(d, d+V, INF); 81 fill(inque, inque+V, false); 82 fill(n, n+V, 0); 83 queue<int> que; 84 d[0]=0; 85 que.push(0); 86 n[0]++; 87 inque[0]=true; 88 while (!que.empty()) 89 { 90 int v=que.front();que.pop(); 91 inque[v]=false; 92 for (int i=0; i<G[v].size(); i++) 93 { 94 if (d[G[v][i].to]>d[v]+G[v][i].cost) 95 { 96 d[G[v][i].to]=d[v]+G[v][i].cost; 97 if (!inque[G[v][i].to]) 98 { 99 que.push(G[v][i].to); 100 inque[G[v][i].to]=true; 101 if (++n[G[v][i].to]>V) return false; 102 } 103 } 104 } 105 } 106 return true; 107 }
标签:turn between term cer i+1 number lang iss location
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9461702.html