黑书 双调路径

http://acm.cs.ecnu.edu.cn/problem.php?problemid=1468

解题报告:http://www.cnblogs.com/blackcruiser/articles/1997097.html

View Code
  1 #include<iostream>

  2 #include<cstdio>

  3 #include<cstring>

  4 #include<algorithm>

  5 #include<cmath>

  6 #include<queue>

  7 #include<stack>

  8 #include<string>

  9 #include<vector>

 10 #include<cstdlib>

 11 #include<map>

 12 #include<set>

 13 using namespace std;

 14 #define CL(x,v) memset(x,v,sizeof(x));

 15 #define R(i,st,en) for(int i=st;i<en;++i)

 16 #define LL long long

 17 #define inf 0x3f3f3f3f

 18  

 19 const int maxn = 101;

 20 const int maxm = 1000;

 21 int n,m,st,en;

 22 struct edge

 23 {

 24     int v,c,t;

 25     edge(){}

 26     edge(int v, int c, int t):v(v), c(c), t(t){}

 27 };

 28 vector <edge> adj[maxn];

 29 int dis[maxn][maxn * 100 + 1];

 30 bool vis[maxn][maxn * 100 + 1];

 31 int minc[maxn],MAX_C;

 32 struct node

 33 {

 34     int u,c,t;

 35     bool operator < (const node & oth) const

 36     {

 37         return t > oth.t;

 38     }

 39     node(){}

 40     node(int u, int c, int t):u(u), c(c), t(t){}

 41 };

 42 int dij()

 43 {

 44     priority_queue <node> q;

 45     dis[st][0] = 0;

 46     q.push(node(st, 0, 0));

 47     while (!q.empty())

 48     {

 49         node cur = q.top();

 50         q.pop();

 51         if(vis[cur.u][cur.c]) continue;

 52         vis[cur.u][cur.c] = 1;

 53         if(minc[cur.u] < cur.c) continue;

 54         minc[cur.u] = cur.c;

 55         for (int i = 0; i < adj[cur.u].size(); ++i)

 56         {

 57             node nt;

 58             nt.u = adj[cur.u][i].v;

 59             nt.t = cur.t + adj[cur.u][i].t;

 60             nt.c = cur.c + adj[cur.u][i].c;

 61             if (nt.c <= MAX_C && nt.t < dis[nt.u][nt.c])

 62             {

 63                 dis[nt.u][nt.c] = nt.t;

 64                 q.push(nt);

 65             }

 66         }

 67     }

 68     int tmp = 0, min_t = inf;

 69     for (int i = 0; i<= MAX_C; ++i)

 70         if (dis[en][i] < inf && min_t > dis[en][i])

 71         {

 72             tmp++;

 73             min_t = dis[en][i];

 74         }

 75     return tmp;

 76 }

 77 void init()

 78 {

 79     memset(vis, 0, sizeof(vis));

 80     memset(dis, 0x3f, sizeof(dis));

 81     memset(minc, 0x3f, sizeof(minc));

 82     MAX_C =  (n - 1) * 100;

 83     memset(adj, 0, sizeof(adj));

 84 }

 85 int main()

 86 {

 87     while(~scanf("%d%d%d%d", &n, &m, &st, &en))

 88     {

 89         init();

 90         for (int i = 0; i < m; ++i)

 91         {

 92             int u, v, c, t;

 93             scanf("%d%d%d%d", &u, &v, &c, &t);

 94             adj[u].push_back(edge(v, c, t));

 95             adj[v].push_back(edge(u, c, t));

 96         }

 97         int ans = dij();

 98         printf("%d\n",ans);

 99     }

100     return 0;

101 }

 

你可能感兴趣的:(路径)