9-17 (水BFS,e24高精度, 最大流最小割, 思路T,卡AC自动机)

Fire! (BFS)

Joe works in a maze. Unfortunately, portions of the maze have
caught on fire, and the owner of the maze neglected to create a fire
escape plan. Help Joe escape the maze.
Given Joe’s location in the maze and which squares of the maze
are on fire, you must determine whether Joe can exit the maze before
the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or
horizontally (not diagonally). The fire spreads all four directions
from each square that is on fire. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.

Input

The first line of input contains a single integer, the number of test
cases to follow. The first line of each test case contains the two
integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The
following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
C characters, and each of these characters is one of:
• #, a wall
• ., a passable square
• J, Joe’s initial position in the maze, which is a passable square
• F, a square that is on fire
There will be exactly one J in each test case.

Output

For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the
fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Sample Output

3
IMPOSSIBLE

两次BFS, 第一次给每个格子标上着火最快的时间点
第二次根据记号,判断Jeo的行进路线

*理清楚

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2010;
int gra[maxn][maxn];
int _time[maxn][maxn];
int flag[maxn][maxn];
int r, c;
char s[maxn];


struct state{
    int step, x, y;
}step[4];
queue<state> ques, quef;

bool judge(int x, int y){
    if(x < 0 || x >= r || y < 0 || y >= c) return false;
    return true;
}

void bfs_fire(){
    while(!quef.empty()){
        state q = quef.front();
        quef.pop();
        for(int i = 0; i < 4; i++){
            int x = q.x + step[i].x, y = q.y + step[i].y;
            if(judge(x, y) && !flag[x][y] && gra[x][y] == 0){
                _time[x][y] = q.step+1;
                flag[x][y] = 1;
                quef.push(state{q.step+1, x, y});
            }
        }
    }
}

int bfs_Joe(){
    memset(flag, 0, sizeof(flag));
    while(!ques.empty()){
        state q = ques.front();
        ques.pop();
        for(int i = 0; i < 4; i++){
            int x = q.x + step[i].x, y = q.y + step[i].y;
            if(!judge(x, y)) return q.step+1;
            if(gra[x][y] == 0 && !flag[x][y] && (_time[x][y] < 0 ||_time[x][y] > q.step+1)){
                flag[x][y] = 1;
                ques.push(state{q.step+1, x, y});
            }
        }
    }
    return -1;
}

int main()
{
    step[0].x = step[1].x = 0;
    step[0].y = -1, step[1].y = 1;
    step[2].y = step[3].y = 0;
    step[2].x = -1, step[3].x = 1;
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &r, &c);
        memset(_time, -1, sizeof(_time));
        memset(flag, 0, sizeof(flag));
        while(!ques.empty()) ques.pop();
        while(!quef.empty()) quef.pop();
        for(int i = 0; i < r; i++){
            scanf("%s", s);
            for(int j = 0; j < c; j++){
                switch (s[j]){ //switch的特性
                    case '#': gra[i][j] = 1; break;
                    case 'J': ques.push(state{0, i, j});
                    case '.': gra[i][j] = 0; break;
                    case 'F': gra[i][j] = -1; flag[i][j] = 1; _time[i][j]=0;
                              quef.push(state{0, i, j});
                }
            }
        }

        bfs_fire();
        int ans = bfs_Joe();
        if(ans < 0) printf("IMPOSSIBLE\n");
        else printf("%d\n", ans);
    }
    return 0;
}

Apple

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Problem Description

Apple is Taotao’s favourite fruit. In his backyard, there are three apple trees with coordinates (x1,y1), (x2,y2), and (x3,y3). Now Taotao is planning to plant a new one, but he is not willing to take these trees too close. He believes that the new apple tree should be outside the circle which the three apple trees that already exist is on. Taotao picked a potential position (x,y) of the new tree. Could you tell him if it is outside the circle or not?

Input

The first line contains an integer T, indicating that there are T(T≤30) cases.
In the first line of each case, there are eight integers x1,y1,x2,y2,x3,y3,x,y, as described above.
The absolute values of integers in input are less than or equal to 1,000,000,000,000.
It is guaranteed that, any three of the four positions do not lie on a straight line.

Output

For each case, output “Accepted” if the position is outside the circle, or “Rejected” if the position is on or inside the circle.

Sample Input

3
-2 0 0 -2 2 0 2 -2
-2 0 0 -2 2 0 0 2
-2 0 0 -2 2 0 1 1

Sample Output

Accepted
Rejected
Rejected

注意数据范围是1e24!
注意数据范围是1e24!
注意数据范围是1e24!

大整数没法处理除得的结果为小数的情况——
书缘学姐提醒的技巧: 把所有需要除以整数的情况都消掉, 只留下分母,接下来的所有步骤同时翻倍

~收得一份大数模板~一份判断一个点是否在一个三角形外接圆内部的模板~

考虑大数模板代码太长,完整的贴在这里:http://paste.ubuntu.com/25556817/
贴出来除了大整数的余下代码:

#include <bits/stdc++.h>
using namespace std;
#define MAX_L 2005 //最大长度,可以修改

class bign
{
public:
    int len, s[MAX_L];//数的长度,记录数组
//构造函数
    bign();
    bign(const char*);
    bign(int);
    bool sign;//符号 1正数 0负数
    string toStr() const;//转化为字符串,主要是便于输出
    friend istream& operator>>(istream &,bign &);//重载输入流
    friend ostream& operator<<(ostream &,bign &);//重载输出流
//重载复制
    bign operator=(const char*);
    bign operator=(int);
    bign operator=(const string);
//重载各种比较
    bool operator>(const bign &) const;
    bool operator>=(const bign &) const;
    bool operator<(const bign &) const;
    bool operator<=(const bign &) const;
    bool operator==(const bign &) const;
    bool operator!=(const bign &) const;
//重载四则运算
    bign operator+(const bign &) const;
    bign operator++();
    bign operator++(int);
    bign operator+=(const bign&);
    bign operator-(const bign &) const;
    bign operator--();
    bign operator--(int);
    bign operator-=(const bign&);
    bign operator*(const bign &)const;
    bign operator*(const int num)const;
    bign operator*=(const bign&);
    bign operator/(const bign&)const;
    bign operator/=(const bign&);
//四则运算的衍生运算
    bign operator%(const bign&)const;//取模(余数)
    bign factorial()const;//阶乘
    bign Sqrt()const;//整数开根(向下取整)
    bign pow(const bign&)const;//次方
//一些乱乱的函数
    void clean();
    ~bign();
};
#define max(a,b) a>b ? a : b
#define min(a,b) a<b ? a : b
//{一系列函数定义}

struct PointTri
{
    bign x;    // x 坐标
    bign y;    // y 坐标
};


// 判断点 P 是否在圆内
bool InnerOROut1(PointTri Vrtx0, PointTri Vrtx1, PointTri Vrtx2, PointTri Vrtx)
{
    bign Radius_2;  // 半径的平方
    bign Cntrx, Cntry; //圆心坐标

    // 求圆心和半径
    bign a1 = (Vrtx1.x - Vrtx0.x), b1 = (Vrtx1.y - Vrtx0.y);
    bign c1 = (a1*a1 + b1*b1) ;  // c1/2
    bign a2 = (Vrtx2.x - Vrtx0.x), b2 = (Vrtx2.y - Vrtx0.y);
    bign c2 = (a2*a2 + b2*b2) ; // c2/2
    bign d = (a1*b2 - a2*b1);
    bign two = "2";
    Cntrx = Vrtx0.x * two * d + (c1 * b2 - c2 * b1) ;  // cntrx/2d
    Cntry = Vrtx0.y * two * d + (a1 * c2 - a2 * c1) ; // cntry/2d

    Radius_2 = (Vrtx0.x * two *d - Cntrx)*(Vrtx0.x * two *d - Cntrx) + (Vrtx0.y*two*d - Cntry)*(Vrtx0.y*two*d - Cntry);

    // 判断 Vrtx0 是否在外接圆内或其上,若是,返回 true,否则,返回 false
    bign Rad_V0Cntr;
    Rad_V0Cntr = (Vrtx.x * two *d - Cntrx)*(Vrtx.x * two *d - Cntrx) + (Vrtx.y*two*d - Cntry)*(Vrtx.y*two*d - Cntry);
    // cout << d << endl;
    // cout << Rad_V0Cntr << endl;
    // cout << Radius_2 << endl;
    if (Rad_V0Cntr <= Radius_2)
    {
        return true;
    } 
    else
    {
        return false;
    }
}



int main()
{
    PointTri p0, p1, p2, p;
    int t;
    scanf("%d", &t);
    while(t--){
        cin >> p0.x >> p0.y >> p1.x >> p1.y >> p2.x >> p2.y >> p.x >> p.y;
        if (InnerOROut1(p0, p1, p2, p))
        {
            cout << "Rejected" << endl;
        }
        else
        {
            cout << "Accepted" << endl;
        }
    }
    return 0;
}

Smallest Minimum Cut

Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Problem Description

Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition of nodes set V into two parts such that s and t belong to different parts. The cut set is the subset of E with all edges connecting nodes in different parts. A minimum cut is the one whose cut set has the minimum summation of capacities. The size of a cut is the number of edges in the cut set. Please calculate the smallest size of all minimum cuts.

Input

The input contains several test cases and the first line is the total number of cases T (1≤T≤300).
Each case describes a network G, and the first line contains two integers n (2≤n≤200) and m (0≤m≤1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 1 to n.
The second line contains two different integers s and t (1≤s,t≤n) corresponding to the source and sink.
Each of the next m lines contains three integers u,v and w (1≤w≤255) describing a directed edge from node u to v with capacity w.

Output

For each test case, output the smallest size of all minimum cuts in a line.

Sample Input

2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 3

Sample Output

2
3

给出一个有向图和起始点, 求边数最少的最小割
这个是原题, 可以参考博客讲解:http://blog.csdn.net/crescent__moon/article/details/44515433
http://blog.csdn.net/u013368721/article/details/28071345

不是很理解什么指最小割不能相互跨立,学习胡伯涛的论文:
《最小割模型》:http://blog.csdn.net/u013368721/article/details/28071345

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 210  //点
#define MAXM 40100//边
#define inf 0x3f3f3f3f
using namespace std;

struct E
{
    int from,v,next;
    int cap;
} edge[MAXM];
int num;
int head[MAXN],dis[MAXN],gap[MAXN];
int nn,n,m;//代表点的个数

void init()
{
    num=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int w)
{
    edge[num].from=u;
    edge[num].v=v;
    edge[num].cap=w;
    edge[num].next=head[u];
    head[u]=num++;
    edge[num].from=v;
    edge[num].v=u;
    edge[num].cap=0;
    edge[num].next=head[v];
    head[v]=num++;
}
void BFS(int start,int end)
{
    memset(dis,-1,sizeof(dis));
    memset(gap,0,sizeof(gap));
    gap[0]=1;
    int que[MAXN],front,rear;
    front=rear=0;
    dis[end]=0;
    que[rear++]=end;
    while(front!=rear)
    {
        int u=que[front++];
        if(front==MAXN)front=0;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]!=-1)continue;
            que[rear++]=v;
            if(rear==MAXN)rear=0;
            dis[v]=dis[u]+1;
            ++gap[dis[v]];
        }
    }
}
int SAP(int start,int end)
{
    int res=0;
    nn=n;
    BFS(start,end);
    int cur[MAXN],S[MAXN];
    memcpy(cur,head,sizeof(head));
    int u=start,i,vp=0;
    while(dis[start]<nn)
    {
        if(u==end)
        {
            int temp=inf;
            int inser;
            for(i=0; i<vp; i++)
                if(temp>edge[S[i]].cap)
                {
                    temp=edge[S[i]].cap;
                    inser=i;
                }
            for(i=0; i<vp; i++)
            {
                edge[S[i]].cap-=temp;
                edge[S[i]^1].cap+=temp;
            }
            res+=temp;
            vp=inser;
            u=edge[S[vp]].from;
        }
        if(u!=end&&gap[dis[u]-1]==0)
            break;
        for(i=cur[u]; i!=-1; i=edge[i].next)
            if(edge[i].cap!=0&&dis[u]==dis[edge[i].v]+1)
                break;
        if(i!=-1)
        {
            cur[u]=i;
            S[vp++]=i;
            u=edge[i].v;
        }
        else
        {
            int min=nn;
            for(i=head[u]; i!=-1; i=edge[i].next)
            {
                if(edge[i].cap==0)continue;
                if(min>dis[edge[i].v])
                {
                    min=dis[edge[i].v];
                    cur[u]=i;
                }
            }
            --gap[dis[u]];
            dis[u]=min+1;
            ++gap[dis[u]];
            if(u!=start)u=edge[S[--vp]].from;
        }
    }
    return res;
}

int main()
{
    int s,t,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d",&n,&m,&s,&t);
        init();
        int u,v,c,sum=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&c);
            addedge(u,v,c*1000+1);
            sum+=c;
        }
        int ans=SAP(s,t);
        if(!ans){printf("0\n");continue;}
        double a,b;
        if(ans%1000==0){b=1000;ans-=1000;}
        else b=ans%1000;
        a=sum-ans/1000;
        printf("%.0f\n",b);
    }
    return 0;
}

Brute Force Sorting

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Problem Description

Beerus needs to sort an array of N integers. Algorithms are not Beerus’s strength. Destruction is what he excels. He can destroy all unsorted numbers in the array simultaneously. A number A[i] of the array is sorted if it satisfies the following requirements.
1. A[i] is the first element of the array, or it is no smaller than the left one A[i−1].
2. A[i] is the last element of the array, or it is no bigger than the right one A[i+1].
In [1,4,5,2,3], for instance, the element 5 and the element 2 would be destoryed by Beerus. The array would become [1,4,3]. If the new array were still unsorted, Beerus would do it again.
Help Beerus predict the final array.

Input

The first line of input contains an integer T (1≤T≤10) which is the total number of test cases.
For each test case, the first line provides the size of the inital array which would be positive and no bigger than 100000.
The second line describes the array with N positive integers A[1],A[2],⋯,A[N] where each integer A[i] satisfies 1≤A[i]≤100000.

Output

For eact test case output two lines.
The first line contains an integer M which is the size of the final array.
The second line contains M integers describing the final array.
If the final array is empty, M should be 0 and the second line should be an empty line.

Sample Input

5
5
1 2 3 4 5
5
5 4 3 2 1
5
1 2 3 2 1
5
1 3 5 4 2
5
2 4 1 3 5

Sample Output

5
1 2 3 4 5
0

2
1 2
2
1 3
3
2 3 5

处理出每一段升序的数段, 每一轮删除相邻数段,相邻的两个数字, 复杂度为o(n)
学姐想的已经很接近了,很可惜

The Dominator of Strings

Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Problem Description

Here you have a set of strings. A dominator is a string of the set dominating all strings else. The string S is dominated by T if S is a substring of T.

Input

The input contains several test cases and the first line provides the total number of cases.
For each test case, the first line contains an integer N indicating the size of the set.
Each of the following N lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of 100000.
The limit is 30MB for the input file.

Output

For each test case, output a dominator if exist, or No if not.

Sample Input

3
10
you
better
worse
richer
poorer
sickness
health
death
faithfulness
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
5
abc
cde
abcde
abcde
bcde
3
aaaaa
aaaab
aaaac

Sample Output

youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
abcde
No

不知道是怎么,反正就是卡AC自动机
有人用好一点的AC自动机模板过了, 有人暴力用KMP或者Trie树过的, 反正自动机就是不对,也是醉
//补题Trie树版

#include <bits/stdc++.h>
using namespace std;
#define id(s) (s - 'a')

struct trie{
    trie* next[26];
    int end;
    trie(){
        for(int i = 0;i < 26; i++) next[i] = NULL;
        end = 0;
    }
}t[100010];
trie f;
trie *head = &f;
int tot;

void init(){
    tot = 0;
    for(int i = 0; i < 26; i++)
        head->next[i] = NULL;
}

void insert(char s[]){
    int n = strlen(s);
    trie* now = head;
    for(int i = 0; i < n; i++){
        if(!now->next[id(s[i])]){
            t[tot] = trie();
            now->next[id(s[i])] = &t[tot++];
        }
        now = now->next[id(s[i])];
    }
    now->end += 1;  
}

int query(char s[]){
    int len = strlen(s);    
    trie* now;
    int res = 0;
    for(int i = 0; i < len; i++){
        now = head;
        int j = i;
        while(j < len && now->next[id(s[j])] != NULL){
            now = now->next[id(s[j])];          
            if(now->end) {
                res += now->end;
                now->end = 0;
            }
            j++;
        }
    }
    return res;
}

char s[100010], str[100010];
int mark;

int main()
{   
    int t;
    scanf("%d", &t);
    while(t--){
        int n;
        init();
        scanf("%d", &n);
        mark = 0;
        int len = 0;
        for(int i = 0; i < n; i++){
            scanf("%s", s);
            int t = strlen(s);
            if(t > len){
                strcpy(str, s);
                len = t;
            }
            insert(s);
        }
        int ans = query(str);       
        if(ans == n) printf("%s\n", str);
        else printf("No\n") ;
    }
    return 0;
}

bitset, Trie树, 后缀数组, 最小割, 知识点要学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值