标签:
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1922 题面废话真多我就删掉一部分……
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <queue> 6 #define rep(i,l,r) for(int i=l; i<=r; i++) 7 #define clr(x,y) memset(x,y,sizeof(x)) 8 #define travel(x) for(Edge *p=last[x]; p; p=p->pre) 9 using namespace std; 10 const int INF = 0x3f3f3f3f; 11 const int maxn = 3010; 12 int n,m,x,y,z,d1[maxn],d2[maxn],protect[maxn]; 13 bool vis[maxn]; 14 struct Edge{ 15 Edge *pre; 16 int to,cost; 17 }edge[100010]; 18 Edge *last[maxn],*pt; 19 struct node{ 20 int x,d; 21 node(int _x,int _d) : x(_x), d(_d){} 22 inline bool operator < (const node &_Tp) const { 23 return d > _Tp.d; 24 } 25 }; 26 priority_queue <node> q; 27 inline int read(){ 28 int ans = 0, f = 1; char c = getchar(); 29 while (!isdigit(c)){ 30 if (c == ‘-‘) f = -1; c = getchar(); 31 } 32 while (isdigit(c)){ 33 ans = ans * 10 + c - ‘0‘; c = getchar(); 34 } 35 return ans * f; 36 } 37 inline void addedge(int x,int y,int z){ 38 pt->pre = last[x]; pt->to = y; pt->cost = z; last[x] = pt++; 39 } 40 void init(){ 41 n = read(); m = read(); clr(last,0); pt = edge; 42 rep(i,1,m){ 43 x = read(); y = read(); z = read(); 44 addedge(x,y,z); 45 } 46 rep(i,1,n){ 47 x = read(); protect[i] = x; 48 rep(j,1,x) y = read(), addedge(y,i,0); 49 } 50 } 51 void dijkstra(){ 52 clr(d1,INF); d1[1] = 0; q.push(node(1,0)); clr(vis,0); 53 while (!q.empty()){ 54 node now = q.top(); q.pop(); 55 if (vis[now.x]) continue; vis[now.x] = 1; 56 travel(now.x){ 57 if (!p->cost){ 58 protect[p->to]--; d2[p->to] = max(d2[p->to],d1[now.x]); 59 d1[p->to] = max(d1[p->to],d2[p->to]); 60 if (!protect[p->to]) q.push(node(p->to,d1[p->to])); 61 } 62 else{ 63 if (d1[p->to] > d1[now.x] + p->cost){ 64 d1[p->to] = d1[now.x] + p->cost; 65 if (!protect[p->to]) q.push(node(p->to,d1[p->to])); 66 } 67 } 68 } 69 } 70 printf("%d\n",d1[n]); 71 } 72 int main(){ 73 init(); 74 dijkstra(); 75 return 0; 76 }
标签:
原文地址:http://www.cnblogs.com/jimzeng/p/bzoj1922.html