spoj_15_SHPATH
2012-04-06 11:11
155 查看
题目数据范围比较高,用map记录城市,再用dijkstra+heap既可以了AC了
/* *NAME:SHPATH *LANG:C++ *Source:spoj15 */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cctype> #include <string> #include <map> #define INF 0x7FFFFFFF using namespace std; int n,m,s,hl,start,end,k; string name,startStr,endStr; map<string,int> city;//用来储存名称于城市编号的对应关系 struct heapNode{ int v,d; }heap[10101]; struct node{ int v,w; node *next; }; node *g[10101]; int hpos[10101]; inline void insert(int a,int b,int w){ node *x= new node; x->v=b;x->w=w;x->next=g[a];g[a]=x; } inline void init(){ city.clear(); memset(hpos,0,sizeof(hpos)); memset(g,0,sizeof(g)); memset(heap,0,sizeof(heap)); for (int i=1;i<=n;++i){ cin >> name; city[name]=i; cin >> m; for (int j=1;j<=m;++j){ int x,y; cin >> x >> y; insert(i,x,y); } } } inline void swap(int x,int y){ heap[0]=heap[x];heap[x]=heap[y];heap[y]=heap[0]; hpos[heap[x].v]=x;hpos[heap[y].v]=y; } inline void heapUp(int i){ while (i!=1 && heap[i].d<heap[i/2].d){ swap(i,i/2); i/=2; } } inline void heapDown(){ int i=2; while (i<=hl){ if (i<hl && heap[i].d>heap[i+1].d) ++i; if (heap[i].d<heap[i/2].d){ swap(i,i/2); i*=2; } else break; } } inline void relax(int u,int v,int w){ if (heap[hpos[u]].d+w<heap[hpos[v]].d){ heap[hpos[v]].d=heap[hpos[u]].d+w; heapUp(hpos[v]); } } inline void dijkstra(){ int u;node *p = 0; for (int u=1;u<=n;++u){ heap[u].v=u; heap[u].d=INF; hpos[u]=u; } heap[start].d=0;swap(1,start); hl=n; while (hl>0){ u=heap[1].v; swap(1,hl);hl--;heapDown(); p=g[u]; while (p!=0){ if (hpos[p->v]<=hl) relax(u,p->v,p->w); p=p->next; } } } inline void print(){ cout << heap[hpos[end]].d << endl; } int main(){ std::ios::sync_with_stdio(false); cin >> s; while (s--){ cin >> n; init(); cin >> k; for (int i=1;i<=k;++i){ cin >> startStr >> endStr; start=city[startStr];end=city[endStr]; dijkstra(); print(); } } }
相关文章推荐
- SPOJ:416 Divisibility by 15
- SPOJ 416 Divisibility by 15 细节题
- SPOJ 15. The Shortest Path 堆优化Dijsktra
- SPOJ 416 - Divisibility by 15(贪心)
- SPOJ 416. Divisibility by 15
- SPOJ - DIV15(数论)
- SPOJ-SHPATH - The Shortest Path
- SPOJ 15. The Shortest Path 最短路径题解
- SPOJ 416 - Divisibility by 15(贪心)
- SPOJ 15. The Shortest Path 最短路径题解
- 最短路径spoj15 The Shortest Path
- SPOJ 15. The Shortest Path 堆优化Dijsktra
- SPOJ 1421 - Goods(置换)
- 05-1. 约分最简分式(15)
- 每天一个linux命令(15):tail 命令
- PAT:B1046. 划拳(15)
- spoj 78
- 每天一个linux命令(15):tail 命令
- SPOJ_PT07Z 树最长路径问题
- 写一个函数返回参数二进制中 1 的个数 比如: 15 0000 1111 4 个 1