1003:

并查集在处理矛盾关系的应用,讲的比较好的题解

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <vector>
#include <bitset>
#include <cstdio>
#include <string>
#include <numeric>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};//up down left right
bool inmap(int x,int y,int n,int m){if(x<1||x>n||y<1||y>m)return false;return true;}
int hashmap(int x,int y,int m){return (x-1)*m+y;} #define eps 1e-8
#define inf 0x7fffffff
#define debug puts("BUG")
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define N 11111 map<int,int>mp;
struct node
{
int l,r;
char c;
}nd[N>>1];
int fa[N<<1];
int find(int x)
{
if (fa[x]!=x)
fa[x] = find(fa[x]);
return fa[x];
} int gao(int m,int cnt)
{
for (int i = 0; i < m; ++i)
{
int l = mp[nd[i].l-1], r= mp[nd[i].r];
char c = nd[i].c;
int f1 = find(l), f2 = find(r), f3 = find(l+cnt), f4 = find(r+cnt);
if (c == 'e')
{
if (f1 == f4 && f2 == f3)
return i;
fa[f1] = f2;
fa[f3] = f4;
}
else
{
if (f1 == f2)
return i;
fa[f1] = f4;
fa[f2] = f3;
}
}
return m;
}
int main()
{
//read;
int n,m;
while (~scanf("%d",&n))
{
if (n == -1) break;
scanf("%d",&m);
mp.clear();
int l, r, cnt = 0;
char str[11];
for (int i = 0; i < m; ++i)
{
scanf("%d%d%s",&l,&r,str);
nd[i].l = l, nd[i].r = r;
nd[i].c = str[0];
if (mp.find(l-1) == mp.end())
mp[l-1] = cnt++;
if (mp.find(r) == mp.end())
mp[r] = cnt++;
}
for (int i = 0; i < 2*cnt; ++i)
fa[i] = i;
int ans = gao(m,cnt);
printf("%d\n", ans);
}
return 0;
}

 

1004:

能够加深对floyd理解的好题,当然还有另外的做法

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <vector>
#include <bitset>
#include <cstdio>
#include <string>
#include <numeric>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};//up down left right
bool inmap(int x,int y,int n,int m){if(x<1||x>n||y<1||y>m)return false;return true;}
int hashmap(int x,int y,int m){return (x-1)*m+y;} #define eps 1e-8
#define inf 0x7ffffff
#define debug puts("BUG")
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define N 155
int mp[N][N];
int dis[N][N];
int nxt[N][N];
stack<int>st;
int floyd(int n)
{
int ans = inf;
for (int k = 1; k <= n; ++k)
{
for (int i = 1; i < k; ++i)
for (int j = i+1; j < k; ++j)
{
if (ans > dis[i][j] + mp[k][i] + mp[j][k])
{
ans = dis[i][j] + mp[k][i] + mp[j][k];
while (!st.empty())
st.pop();
for (int t = i; t != j; t = nxt[t][j])
st.push(t);
st.push(j);
st.push(k);
}
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
{
if (dis[i][k] == inf || dis[k][j] == inf)
continue;
if (dis[i][k] + dis[k][j] < dis[i][j])
{
dis[i][j] = dis[i][k] + dis[k][j];
nxt[i][j] = nxt[i][k];
}
}
}
return ans;
}
int main()
{
//read;
int n,m;
while (~scanf("%d",&n))
{
if (n == -1)break;
scanf("%d",&m);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
{
dis[i][j] = mp[i][j] = inf;
nxt[i][j] = j;
}
int u,v,c;
for (int i = 0; i < m; ++i)
{
scanf("%d%d%d",&u, &v, &c);
if (mp[u][v] > c)
mp[u][v] = mp[v][u] = dis[u][v] = dis[v][u] = c;
}
int ans = floyd(n);
//printf("%d\n",ans);
if (ans == inf)
puts("No solution.");
else
{
bool f = false;
while (!st.empty())
{
if (f) printf(" ");
else f = true;
printf("%d",st.top());
st.pop();
}puts("");
}
}
return 0;
}

URAL 1003,1004的更多相关文章

  1. SOJ 1002/1003/1004 大整数相加/相乘/相除

    三个题目分别考察大整数相加相乘相除运算.如果按照传统算法是取一个长数组,之后进行模拟或者FFT来进行运算.但是相对繁琐. 后来昨天的青岛区域赛网赛1001,用到了JAVA的BigDecimal,于是反 ...

  2. 51Nod 1003 1004 1009

    1003 阶乘后面0的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 n的阶乘后面有多少个0? 6的阶乘 = 1*2*3*4*5*6 = 720,720后面有1 ...

  3. URAL - 1003:Parity (带权并查集&2-sat)

    Now and then you play the following game with your friend. Your friend writes down a sequence consis ...

  4. 要back的题目 先立一个flag

    要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...

  5. mysql语句查询练习

                                                                     1.创建students表mysql> create table ...

  6. 2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)(7/10)

    1001题意:n个人,给m对敌对关系,X个好人,Y个坏人.现在问你是否每个人都是要么是好人,要么是坏人. 先看看与X,Y个人有联通的人是否有矛盾,没有矛盾的话咋就继续遍历那些不确定的人关系,随便取一个 ...

  7. PHP数组合并+与array_merge的区别分析 & 对多个数组合并去重技巧

    PHP中两个数组合并可以使用+或者array_merge,但之间还是有区别的,而且这些区别如果了解不清楚项目中会要命的! 主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意 ...

  8. POJ推荐50题

    此文来自北京邮电大学ACM-ICPC集训队 此50题在本博客均有代码,可以在左侧的搜索框中搜索题号查看代码. 以下是原文: POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求, ...

  9. 为什么operator>>(istream&, string&)能够安全地读入长度未知的字符串?

    一般而言,实现"读入用户输入的字符串",程序中自然不能对用户输入的长度有所限定.这在C++中很容易实现,而在C中确没那么容易. 这一疑问,我在刚学C++的时候也在脑中闪现过:不过很 ...

随机推荐

  1. LeetCode(55)Jump Game

    题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  2. Uva 816 Abbott的复仇(三元组BFS + 路径还原)

    题意: 有一个最多9*9个点的迷宫, 给定起点坐标(r0,c0)和终点坐标(rf,cf), 求出最短路径并输出. 分析: 因为多了朝向这个元素, 所以我们bfs的队列元素就是一个三元组(r,c,dir ...

  3. 笔记——collections模块

    collections模块 collections模块在内置数据类型(dict.list.set.tuple)的基础上,还提供了几个额外的数据类型:ChainMap.Counter.deque.def ...

  4. jsp获取绝对路径----${pageContext.request.contextPath}

    JSP取得绝对路径 在JavaWeb开发中,常使用绝对路径的方式来引入JavaScript和CSS文件,这样可以避免因为目录变动导致引入文件找不到的情况,常用的做法如下: 一.使用${pageCont ...

  5. 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction

    http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...

  6. 【IntelliJ 】IntelliJ IDEA 15 创建maven项目

    说明 创建Maven项目的方式:手工创建 好处:参考IntelliJ IDEA 14 创建maven项目二(此文章描述了用此方式创建Maven项目的好处)及idea14使用maven创建web工程(此 ...

  7. codeforces 691F(组合数计算)

    Couple Cover, a wildly popular luck-based game, is about to begin! Two players must work together to ...

  8. 显示锁ReentrantLock和Condition的使用

    一.ReentrantLock (1).java.util.concurrent.locks包中的ReentrantLock就是重入锁,它实现了Lock接口,Lock加锁和解锁都是显示的.Reentr ...

  9. FJNUOJ1158(莫比乌斯反演)

    题目:给定n个数字a1...an.有m个询问,格式为L R X Y,意为求aL到aR之间与x的最大公因数为y的个数. 数据组数T<=20 1<=n,m<=1e5 1<=ai&l ...

  10. UVALive7042(博弈论)

    题意: Bob和Alice在有向图内玩游戏,n个顶点,m条边. 每人一颗棋子,初始位置分别是x,y. Bob先手,轮流操作,每次只能走一条有向边. 结束条件: 1.不能操作的人输 2.两个棋子重合Bo ...