↓ 跳过正文
  1. Posts/

ural 1416. Confidential (次小生成树模板题)

·730 字·2 分钟

URAL1416 题意:次小生成树模板题

思路:用Kruskal求最小生成树,标记用过的边。求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就是次小的生成树。复杂度o(m2)。。。貌似有其他优化。。。

写的时候。。因为点数是500。。我把边集的数组大小开成了500.。。交了10遍越界才意识到问题在哪里。。。真的是智商掉线啊orz…

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年07月11日 星期一 20时44分28秒
  4File Name :code/ural/1416.cpp
  5************************************************ */
  6#include <cstdio>
  7#include <cstring>
  8#include <iostream>
  9#include <algorithm>
 10#include <vector>
 11#include <queue>
 12#include <set>
 13#include <map>
 14#include <string>
 15#include <cmath>
 16#include <cstdlib>
 17#include <ctime>
 18#define fst first
 19#define sec second
 20#define lson l,m,rt<<1
 21#define rson m+1,r,rt<<1|1
 22#define ms(a,x) memset(a,x,sizeof(a))
 23typedef long long LL;
 24#define pi pair < int ,int >
 25#define MP make_pair
 26using namespace std;
 27const double eps = 1E-8;
 28const int dx4[4]={1,0,0,-1};
 29const int dy4[4]={0,-1,1,0};
 30const int inf = 0x3f3f3f3f;
 31const int N=505;
 32int n,m;
 33int f[N];
 34int mst;
 35int ans;
 36int cnt = 0 ;
 37vector < pi > e[N*N];
 38bool flag;
 39struct Edge
 40{
 41    int u,v;
 42    int w;
 43    int in;//标记边是否在生成树中。
 44    bool operator < (Edge b)const
 45    {
 46	return w<b.w;
 47    }
 48    void input()
 49    {
 50	scanf("%d%d%d",&u,&v,&w);
 51    }
 52}E[N*N];
 53void init()
 54{
 55    for ( int i = 1 ; i <= n ; i++) f[i] = i;
 56    for ( int i = 1 ; i <= n ; i++) e[i].clear();
 57}
 58int root ( int x)
 59{
 60  //  cout<<"x:"<<x<<" f[x]:"<<f[x]<<endl;
 61    if (x!=f[x]) f[x] = root (f[x]);
 62    return f[x];
 63}
 64void merge( int x,int y)
 65{
 66    int rx = root (x);
 67    int ry = root (y);
 68    if (rx==ry) return ;
 69    f[rx] = ry;
 70  //  if (rx<ry) f[ry] = rx;
 71  //  else f[rx]=ry;
 72}
 73void kruskal(int k)
 74{
 75    for ( int i = 1 ; i <= n ; i++) f[i] = i ;
 76    mst = 0 ;
 77    cnt =  0;
 78    for ( int i = 1; i <= m ; i++)
 79    {
 80	int u = E[i].u;
 81	int v = E[i].v;
 82	int w = E[i].w;
 83	if (i==k) continue;
 84	if (root(u)==root(v)) continue;
 85	merge(u,v);
 86	cnt++;
 87	mst+=w;
 88	if (cnt>=n-1) break;
 89    }
 90    if (cnt<n-1) return ;
 91    ans = min(ans,mst);
 92}
 93int main()
 94{
 95	#ifndef  ONLINE_JUDGE
 96	freopen("code/in.txt","r",stdin);
 97  #endif
 98	cin>>n>>m;
 99	init();
100	for ( int i = 1 ; i <= m ; i++)
101	{
102	    E[i].input();
103	    int u = E[i].u;
104	    int v = E[i].v;
105	    int w = E[i].w;
106
107	    e[u].push_back(make_pair(v,w));
108	    e[v].push_back(make_pair(u,w));
109	}
110	sort(E+1,E+m+1);
111	mst = 0 ;
112	cnt = 0 ;
113	for ( int i = 1 ; i <= m ; i++)
114	{
115	    int u = E[i].u;
116	    int v = E[i].v;
117	    int w = E[i].w;
118	    if (root(u)==root(v)) continue;
119	    E[i].in = 1;
120	    merge(u,v);
121	    cnt++;
122	    mst+=w;
123	}
124	if (cnt<n-1)
125	{
126	    puts("Cost: -1");
127	    puts("Cost: -1");
128	    return 0;
129	}
130	printf("Cost: %d\n",mst);
131	ans = inf;
132	for ( int i = 1 ; i <= m ; i++)
133	{
134	    if (E[i].in==0) continue;
135	   // cout<<"hhh?"<<endl;
136	    kruskal(i);
137	}
138	if (ans==inf) ans = -1;
139	printf("Cost: %d\n",ans);
140  #ifndef ONLINE_JUDGE
141  fclose(stdin);
142  #endif
143    return 0;
144}

相关文章

BZOJ 1681: [Usaco2005 Mar]Checking an Alibi 不在场的证明 (spfa)

·1064 字·3 分钟
1681: [Usaco2005 Mar]Checking an Alibi 不在场的证明 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 250 Solved: 178 [Submit][Status][Discuss] Description # A crime has been comitted: a load of grain has been taken from the barn by one of FJ’s cows. FJ is trying to determine which of his C (1 <= C <= 100) cows is the culprit. Fortunately, a passing satellite took an image of his farm M (1 <= M <= 70000) seconds before the crime took place, giving the location of all of the cows. He wants to know which cows had time to get to the barn to steal the grain. Farmer John’s farm comprises F (1 <= F <= 500) fields numbered 1..F and connected by P (1 <= P <= 1,000) bidirectional paths whose traversal time is in the range 1..70000 seconds (cows walk very slowly). Field 1 contains the barn. It takes no time to travel within a field (switch paths). Given the layout of Farmer John’s farm and the location of each cow when the satellite flew over, determine set of cows who could be guilty. NOTE: Do not declare a variable named exactly ’time’. This will reference the system call and never give you the results you really want.

poj 2342 Anniversary party (基础树形dp)

·634 字·2 分钟
题目链接 题意:n个人的上下级关系形成一棵树..每一个人有一个val(可正可负),要选若干个人参加一个party,要求是一个人和他的直接上级不能同时在场。问参加party的人最大的val之和。

华科软院计组概念复习

·2489 字·5 分钟
noip 初赛加强版既视感… 自己手动整理的 第二章 机器数:正负符号数码化后的数据称为机器数。 BCD 码:用二进制编码的十进制数称为 BCD 码。 有权码:每位二进制数码元都有确定权值的编码。 校验码:为了发现或纠正数据传送中出现错误的编码。 浮点数的精度由尾数的位数决定。 第三章 溢出:运算结果超出了机器能表示的数据范围。 溢出的特征:结果的符号与操作数的符号不同。 变形补码:两个符号位的补码(用来检测溢出,00,11说明没有溢出,10,01说明有溢出) 对阶:使阶码相等的过程(原则是小阶码向大阶码看齐) 结果规格化:将非规格化数处理为规格化形式。

codeforces 660 C. Hard Process (ruler)

·321 字·1 分钟
cf660C solution:ruler.1A 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年06月08日 星期三 23时43分18秒 4File Name :code/cf/problem/660C.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=3E5+7; 34int n,k; 35int sum[N],a[N]; 36 37void ruler() 38{ 39 int head = 1; 40 int tail = 1; 41 int l,r; 42 int res = -1; 43 int cnt = 0 ; 44 45 while (tail<=n) 46 { 47 while (a[tail]==1) tail++; 48// cout<<"head:"<<head<<" tail:"<<tail<<endl; 49 if (a[tail]==0&&tail<=n) cnt++; 50 51 while (sum[tail]-sum[head-1]<=k&&tail<=n) tail++; 52// cout<<"head:"<<head<<"tail:"<<tail<<endl; 53 if (tail-head>res) 54 { 55 res = tail-head; 56 // cout<<"res:"<<res<<endl; 57 l = head; 58 r = tail-1; 59 } 60 61 while (head<=tail&&sum[tail]-sum[head-1]>k) head++; 62// cout<<"head::"<<head<<" tail:"<<tail<<endl; 63 if (tail<=n&&tail-head+1>res) 64 { 65 res = tail-head+1; 66 l = head; 67 r = tail; 68 } 69 70 71 } 72 73 74 for ( int i = l ; i <= r ; i++) a[i] = 1; 75 76 cout<<res<<endl; 77 for ( int i = 1 ; i <= n ; i++) cout<<a[i]<<" "; 78} 79int main() 80{ 81 #ifndef ONLINE_JUDGE 82 freopen("code/in.txt","r",stdin); 83 #endif 84 85 cin>>n>>k; 86 87 sum[0] = 0; 88 for ( int i = 1; i <= n ; i++) 89 { 90 scanf("%d",&a[i]); 91 sum[i] = sum[i-1] +(1-a[i]); 92 } 93 94 ruler(); 95 96 97 #ifndef ONLINE_JUDGE 98 fclose(stdin); 99 #endif 100 return 0; 101}

(转)树形dp题目集

·2713 字·6 分钟
树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树间能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树、三叉树、静态搜索树、AVL树,线段树、SPLAY树,后缀树等等.. 枚举那么多种数据结构只是想说树方面的内容相当多,本专辑只针对在树上的动态规划,即树形DP.做树形DP一般步骤是先将树转换为有根树,然后在树上进行深搜操作,从子节点或子树中返回信息层层往上更新至根节点。这里面的关键就是返回的信息部分,这个也没一般性的东西可讲,因为每道题目要求做的事都不尽相同。