HDU 2122 Ice_cream’s world III
2016-03-28 20:46
316 查看
Description
ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every
city to the capital. The project’s cost should be as less as better.
Input
Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.
Output
If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
Sample Input
Sample Output
PS:注意输出格式,一个答案两个换行;
Prime算法:
在图中任选一点作为开始节点,把这个顶点加入已经访问节点的集合。
求出这样一点的相邻节点的距离。并找出最短的一条边。这是第一步,找到的第一条边。把这一个节点加入已经访问过的集合。
在已经访问过的集合中,再次寻找最短的一条边。这一次还要考虑之前加入的顶点。因为之前的一个顶点到所有的距离已经求出。这一次就可以判断此点到其他点的距离,若距离小于之前对应的距离,则更新;
所以这个dijkstra的变形,只不过权值发生了替换成了累加;
AC代码:(Prime算法)
Kruskal
AC代码:
ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every
city to the capital. The project’s cost should be as less as better.
Input
Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.
Output
If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
Sample Input
2 1 0 1 10 4 0
Sample Output
10 impossible
PS:注意输出格式,一个答案两个换行;
Prime算法:
在图中任选一点作为开始节点,把这个顶点加入已经访问节点的集合。
求出这样一点的相邻节点的距离。并找出最短的一条边。这是第一步,找到的第一条边。把这一个节点加入已经访问过的集合。
在已经访问过的集合中,再次寻找最短的一条边。这一次还要考虑之前加入的顶点。因为之前的一个顶点到所有的距离已经求出。这一次就可以判断此点到其他点的距离,若距离小于之前对应的距离,则更新;
所以这个dijkstra的变形,只不过权值发生了替换成了累加;
AC代码:(Prime算法)
#include <bits/stdc++.h> #define MAX 999999999 using namespace std ; int s , t , c ,ans , ma[10010][10010],vis[10010],dis[10010],n,m; void dijk() { int mini , k ,flag; ans =flag =0 ; for(int i = 1 ; i < m;i++) { dis[i]=ma[0][i]; } vis[0]=1; for(int i = 1 ; i < m ; i++) { mini = MAX ; for(int j = 0 ; j <m;j++ ) { if(!vis[j]&&mini>dis[j]) { mini = dis[j]; k= j ; } } if(mini==MAX) { printf("impossible\n\n"); flag = 1 ; break; } vis[k]=1 ; ans+=mini; for(int j = 0 ; j < m ; j++) { if(!vis[j]&&dis[j]>ma[k][j]) dis[j]=ma[k][j]; } } if(flag==0) printf("%d\n\n",ans); } int main() { while(cin>>m>>n) { for(int i = 0 ; i < m ; i++) { for(int j = 0 ; j < m ; j++) { ma[i][j]=MAX; } ma[i][i]=0; vis[i]=0; dis[i]=MAX; } for(int i = 0 ; i < n ;i++) { int a , b , c ; cin>>a>>b>>c; if(ma[a][b]>c) { ma[a][b]=ma[b][a]=c; } } dijk(); } }
Kruskal
AC代码:
#include <bits/stdc++.h> #define N 2000 #define MAX 10010 using namespace std ; int n , m , pre ; struct node { int from ; int to ; int val; }ma[MAX]; int find(int x ) { if(pre[x]!=x) return pre[x]=find(pre[x]); return x ; } bool cmp(node a, node b ) { return a.val<b.val; } void kru() { int ans , count ; ans = count = 0 ; for(int i = 0 ; i < m ; i++) { int u = find(ma[i].from); int v = find(ma[i].to); if(u!=v) { ans+=ma[i].val; pre[u]=v; //*并查集特性 也可以 pre[v] = u ; count++; } if(count == n - 1 ) break; } if(count == n - 1 ) printf("%d\n\n",ans); else printf("impossible\n\n"); } int main() { while(cin>>n>>m) { memset(ma,0,sizeof(ma)); for(int i = 0 ; i <=n;i++) { pre[i]=i; } for(int i= 0 ; i < m ; i++) { cin>>ma[i].from>>ma[i].to>>ma[i].val; } sort(ma,ma+m,cmp); kru(); } return 0 ; }
相关文章推荐
- spring data jpa 了解以及环境搭建
- C - Train Problem II——(HDU 1023 Catalan 数)
- 顺序表基本运算(线性表)c
- C++中str1::function和bind
- 实验4 颜色、字符串资源的使用 实验报告
- 第三周
- 【NOIP2013提高组day2】【JZOJ 3537】华容道
- linux下so动态库一些不为人知的秘密(上)
- EditText焦点一直在末尾!
- AngularJS 过滤器(filter)
- HDU 2141 can you find it?【二分查找】
- 关于转发与重定向
- 蓝牙学习之旅——低功耗蓝牙之广播报文SCAN_RSP
- hdu 2859 Phalanx 动态规划
- 连续时间系统的时域分析
- OneZero第二周第一次站立会议(2016.3.28)
- Wampserver #1045 无法登录 mysql 服务器
- 声明式事务
- HDU 5044 Tree LCA
- 单链表的逆置