标签:
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1247 Accepted Submission(s): 411
#include <stdio.h> #include <math.h> #include <iostream> #include <algorithm> #include <math.h> #include <queue> #include <string.h> using namespace std; typedef long long ll; const int N = 105; const int INF = 999999999; struct Node { ///枚举高度差所需要用到的结构体 int low,high; }node[N*N]; struct Edge{ int v,w,next; }edge[N*N]; int head[N]; ll h[N]; int graph[N][N]; int n,m; int cmp(Node a,Node b){ return (a.high-a.low)<(b.high-b.low); } bool vis[N]; int d[N]; void addEdge(int u,int v,int w,int &k){ edge[k].v = v,edge[k].w = w; edge[k].next = head[u],head[u]=k++; } void spfa(int s,int low,int high){ queue<int > q; for(int i=1;i<=n;i++){ d[i] = INF; vis[i] = false; } d[s] = 0; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = false; if(h[u]>high||h[u]<low) continue; for(int k = head[u];k!=-1;k=edge[k].next){ int v = edge[k].v,w = edge[k].w; if(h[v]>high||h[v]<low) continue; if(d[v]>d[u]+w){ d[v] = d[u]+w; if(!vis[v]){ vis[v]=true; q.push(v); } } } } } int main() { int tcase; scanf("%d",&tcase); while(tcase--){ memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%lld",&h[i]); } int tot = 0; for(int i=1;i<=m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); addEdge(a,b,c,tot); addEdge(b,a,c,tot); } int k = 0; for(int i=1;i<=n;i++){ for(int j=i;j<=n;j++){ if(h[i]<h[j]){ node[k].low = h[i]; node[k++].high = h[j]; } else { node[k].low = h[j]; node[k++].high = h[i]; } } } sort(node,node+k,cmp); for(int i=0;i<k;i++){ spfa(1,node[i].low,node[i].high); if(d[n]<INF){ printf("%d %d\n",node[i].high-node[i].low,d[n]); break; } } } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5508571.html