The Shortest Path in Nya Graph HDU - 4725

题意:

  1. 层与层之间移动需耗费C
  2. 额外边cost 直接给出

坑题spfa 可过 但需将queue定义为全局 不然会TLE

#include 

using namespace std;
const int MAX_V = 200005;
struct edge{
  int v;
  int cost;
  int next;
};
struct edge es[20 * MAX_V];
int lay[MAX_V],d[MAX_V],head[MAX_V];
bool vv[MAX_V],inqueue[MAX_V];
int V,len;
deque q;
void add(int from,int to,int val)
{
    es[len].v=to;
    es[len].cost =val;
    es[len].next = head[from];
    head[from]=len++;
}
void spfa(int s){
  d[s] = 0; inqueue[s] = 1;
  while(!q.empty()) q.pop_back();
  q.push_back(s);
  while (!q.empty()){
    int u = q.front(); q.pop_front();
    inqueue[u] = 0;
    for (int i = head[u];i != -1;i = es[i].next){
      int v = es[i].v;
      if (d[v] > d[u] + es[i].cost){
        d[v] = d[u] + es[i].cost;
        if (!inqueue[v]){
          if (!q.empty() && d[v] < d[q.front()])
            q.push_front(v);
          else
            q.push_back(v);
          inqueue[v] = 1;
        }
      }
    }
  }
}

void init(){
  memset(head,-1,sizeof head);
  memset(vv,0,sizeof vv);
  memset(d,0x3f, sizeof(d));
}
int main()
{
  int T;
  scanf("%d",&T);
  memset(inqueue,0,sizeof inqueue);
  for (int cnt = 1;cnt<=T;cnt++){
    init();
    int M,W;
    scanf("%d%d%d",&V,&M,&W);
    len = 0;
    for (int i = 1;i<=V;i++){
      int t;
      scanf("%d",&t);
      lay[i] = t;
      vv[t] = 1;
    }
    for (int i = 1;i1) add(i,V+lay[i]-1,W);
      if(lay[i]

你可能感兴趣的:(acm,最短路)