Big Christmas Tree POJ - 3013 一种最短路的出题姿势
2017-10-06 10:18
519 查看
题意:给n个点从1到n标号,下面一行是每个点的权,另外给出m条边,下面是每条边的信息,两个端点+权值,边是无向边。你的任务是选出一些边,使这个图变成一棵树。这棵树的花费是这样算的,1号固定为树根,树中每个父节点下面的边都有个边权,然后边权乘上这条边的下面所有的子孙后代的点权和.
题目换一个角度就会发现其实就是一个裸的最短路,即答案也是每个点权乘上从起点到终点的边权和,即是最短路
代码如下:
题目换一个角度就会发现其实就是一个裸的最短路,即答案也是每个点权乘上从起点到终点的边权和,即是最短路
代码如下:
/* * Author : Echo * Email : 1666424499@qq.com * Description : * Created Time : 2017/10/6 9:26:12 * Last Modify : 2017/10/6 10:11:48 * File Name : a.cpp */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <time.h> #define LL long long #define mem(a,k) memset(a,k,sizeof(a)) using namespace std; const int maxint = -1u>>1; const int maxn=1e5; const int maxm=1e6; bool vis[maxn]; long long dis[maxn]; long long bn[maxn]; struct edge{ int to,next; long long value; void set(int a,long long b,int c){ to=a,value=b;next=c; } }an[maxm]; int cnt=0,head[maxn]; void addedge(int u,int v,long long value){ an[cnt].set(v,value,head[u]); head[u]=cnt++; an[cnt].set(u,value,head[v]); head[v]=cnt++; } void SPFA(int n){ for(int i=1;i<=n;i++){ vis[i]=0; dis[i]=1e17; } vis[1]=1; queue<int> que; dis[1]=0; que.push(1); while(!que.empty()){ int u=que.front(); que.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=an[i].next){ int v=an[i].to; if(dis[v]>dis[u]+an[i].value){ dis[v]=dis[u]+an[i].value; if(!vis[v])que.push(v); vis[v]=1; } } } long long ans=0; for(int i=1;i<=n;i++){ if(dis[i]==1e17){ printf("No Answer\n");return; } ans+=dis[i]*bn[i]; } printf("%lld\n",ans); } int main(){ int t; cin>>t; while(t--){ memset(head,-1,sizeof(head)); cnt=1; int n,m; cin>>n>>m; for(int i=1;i<=n;i++){ scanf("%d",&bn[i]); } for(int i=1;i<=m;i++){ int a,b; long long c; scanf("%d%d%lld",&a,&b,&c); addedge(a,b,c); } SPFA(n); } return 0; }
相关文章推荐
- poj-3013-Big Christmas Tree-求最短路
- poj 3013 Big Christmas Tree(最短路变形)
- POJ-3013 Big Christmas Tree 最短路[推荐]
- POJ 3013 Big Christmas Tree【最短路变形,DIjkstra堆优化+spfa算法】
- poj 3013 big christmas tree 最短路SPFA
- POJ 3013 Big Christmas Tree 最短路
- POJ 3013 Big Christmas Tree (最短路模型)
- poj 3013 Big Christmas Tree 最短路变形
- POJ 3013 Big Christmas Tree (最短路 spfa)
- POJ 3013 Big Christmas Tree (最短路模型)
- poj 3013 Big Christmas Tree 最短路
- POJ 3013 Big Christmas Tree 最短路
- poj 3013 Big Christmas Tree 最短路
- poj 3013 Big Christmas Tree 最短路 (转换思维,看点不看边)
- poj 3013 spfa 最短路 Big Christmas Tree
- 最短路 Big Christmas Tree POJ - 3013
- poj 3013 Big Christmas Tree 最短路 dijkstra算法
- POJ 3013 Big Christmas Tree (SPFA最短路)
- POJ 3013 Big Christmas Tree (最短路)
- 最短路SPFA——Big Christmas Tree ( POJ 3013 )