The Shortest Path in Nya Graph HDU - 4725
2017-12-02 13:06
239 查看
点击打开链接
还是看了题解才懂。。
这道题考的就是抽象建图的思维能力
有n个顶点 挂在不同的层上 不同层之间的定点可互达 同层顶点互不相干 除非有特殊边相连接
一开始暴力建图 先按层数把顶点排序然后二分加边 T了。。
后来想把层抽像出来 层与层,层与该层上的顶点,不同层的顶点 都建双向边 但这是完全错误的 这样会使同层顶点全部可以通过抽象的层顶点连接了 并且第i层与第i+2和第i-2层都可以相连了 从而使顶点跨层相连 而题目只说相邻层才可以相连 不具传递性
应该把一个层拆为两个点 一个负责出 一个负责入 神奇的建图啊。。
还是看了题解才懂。。
这道题考的就是抽象建图的思维能力
有n个顶点 挂在不同的层上 不同层之间的定点可互达 同层顶点互不相干 除非有特殊边相连接
一开始暴力建图 先按层数把顶点排序然后二分加边 T了。。
后来想把层抽像出来 层与层,层与该层上的顶点,不同层的顶点 都建双向边 但这是完全错误的 这样会使同层顶点全部可以通过抽象的层顶点连接了 并且第i层与第i+2和第i-2层都可以相连了 从而使顶点跨层相连 而题目只说相邻层才可以相连 不具传递性
应该把一个层拆为两个点 一个负责出 一个负责入 神奇的建图啊。。
#include <bits/stdc++.h> using namespace std; #define N 0x3f3f3f3f struct nodeI { int v; int w; int next; }; struct nodeII { friend bool operator < (nodeII n1,nodeII n2) { return n1.w>n2.w; } int v; int w; }; priority_queue <nodeII> que; nodeI edge[600010]; int first[300010],dis[300010],book[300010]; int n,m,c,num; void addedge(int u,int v,int w) { edge[num].v=v; edge[num].w=w; edge[num].next=first[u]; first[u]=num++; return; } void calculate() { nodeII cur,tem; int i,u,v,w; while(!que.empty()) que.pop(); memset(dis,0x3f,sizeof(dis)); memset(book,0,sizeof(book)); tem.v=1,tem.w=0; que.push(tem); dis[1]=0; while(!que.empty()) { cur=que.top(); que.pop(); u=cur.v; if(book[u]==1) continue; book[u]=1; for(i=first[u];i!=-1;i=edge[i].next) { v=edge[i].v,w=edge[i].w; if(book[v]==0&&dis[v]>dis[u]+w) { dis[v]=dis[u]+w; tem.v=v,tem.w=dis[v]; que.push(tem); } } } return; } int main() { int t,cas,i,l,u,v,w; scanf("%d",&t); for(cas=1;cas<=t;cas++) { scanf("%d%d%d",&n,&m,&c); memset(first,-1,sizeof(first)); num=0; for(i=1;i<=n;i++) { scanf("%d",&l); addedge(i,n+2*l-1,0); addedge(n+2*l,i,0); } for(i=1;i<n;i++) { addedge(n+2*i-1,n+2*(i+1),c); addedge(n+2*(i+1)-1,n+2*i,c); } /* for(i=1;i<=n;i++) { scanf("%d",&l); addedge(i,n+l,0); addedge(n+l,i,0); } for(i=n+1;i<=n+n;i++) { addedge(i,i-1,c); addedge(i,i+1,c); } */ for(i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } calculate(); if(dis ==N) printf("Case #%d: -1\n",cas); else printf("Case #%d: %d\n",cas,dis ); } return 0; }
相关文章推荐
- HDU 4725 The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph (最短路拆点建图)
- HDU ~ 4725 ~ The Shortest Path in Nya Graph (拆点建图 + Dijkstra)
- HDU 4725 The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph——建图+dijkstra
- 【最短路】 HDU 4725 The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph( 建图 + 最短路 )
- HDU 4725 The Shortest Path in Nya Graph (最短路 )
- The Shortest Path in Nya Graph HDU - 4725
- HDU 4725 The Shortest Path in Nya Graph(拆点+最短路)
- HDU 4725 The Shortest Path in Nya Graph 建图加SPFA
- HDU 4725 The Shortest Path in Nya Graph 【最短路之建图难点】
- HDU 4725 The Shortest Path in Nya Graph
- HDU 4725 —— The Shortest Path in Nya Graph
- hdu 4725 The Shortest Path in Nya Graph
- The Shortest Path in Nya Graph HDU - 4725(最短路,spfa)
- HDU 4725 The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph(最短路)
- hdu-4725-The Shortest Path in Nya Graph-层次网络
- HDU 4725 The Shortest Path in Nya Graph(优先队列+dijkstra)