头像

Hugh.

吉林大学




离线:4小时前


最近来访(26)
用户头像
煎蛋
用户头像
李金严
用户头像
钢铁加鲁鲁
用户头像
zhuzuojun
用户头像
天才小笼包
用户头像
acmdyh
用户头像
ExcenFun
用户头像
Pluviophile
用户头像
井井酒
用户头像
木棉觉
用户头像
Pr
用户头像
IAG丶Skipper
用户头像
小思
用户头像
杜若不知道-
用户头像
wwww_wwww
用户头像
郫县林俊杰
用户头像
william-luo
用户头像
狗头人挖土豆
用户头像
狐闹
用户头像
凡事都可破


Hugh.
1天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

using namespace std;

const int N = 510;

int n,m;
int g[N][N];
int dist[N];
bool st[N];

int dijkstra() // 求1号点到n号点的最短路距离,如果从1号点无法走到n号点则返回-1
{
memset(dist, 0x3f, sizeof dist);
dist[1]=0;
for (int i = 0; i < n-1; i ++ ){
int t=-1;

     for (int j = 1; j <= n; j ++ ){
         if(!st[j] && (t==-1 || dist[t]>dist[j])) t=j;
     }

     for (int j = 1; j <= n; j ++ ){
        dist[j]=min(dist[j],dist[t]+g[t][j]);

 }
st[t] = true;

}
if (dist[n] == 0x3f3f3f3f) return -1;
return dist[n];
}
int main()
{
cin >> n >> m;
memset(g, 0x3f, sizeof g);
while (m – ){
int x,y,z;
cin >> x >> y >> z;
g[x][y]=min(g[x][y],z);
}

cout << dijkstra();

}




Hugh.
1天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

using namespace std;

const int N = 100010,M = 100010;

int n,m;
int h[N], e[M], ne[M], idx;
int d[N],q[N];

void add(int a, int b) // 添加一条边a->b
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

bool topsort(){
int hh=0,tt=-1;

for (int i = 1; i <= n; i ++ ){
    if(d[i]==0) q[++tt]=i;
}

while(hh<=tt){
    int t=q[hh++];
    for (int i = h[t]; i != -1; i = ne[i]){
        int j=e[i];
        if(--d[j]==0) q[++tt]=j;
    }
}
return tt==n-1;

}

int main()
{
cin >> n >>m;
memset(h, -1, sizeof h);
for (int i = 0; i < m; i ) {
int a,b;
cin >> a >> b;
add(a, b);
d[b]
;
}

if(!topsort()) cout << -1;
else{
    for (int i = 0; i < n; i ++ ) cout << q[i] <<' ';


}

}



活动打卡代码 AcWing 844. 走迷宫

Hugh.
2天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

using namespace std;

typedef pair[HTML_REMOVED] PII;

const int N = 110;

int n,m;
int g[N][N],d[N][N];

int bfs(){

queue<PII> q;
memset(d, -1, sizeof d);
d[0][0]=0;
q.push({0,0});
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while(q.size()){

    auto t=q.front();
    q.pop();

    for (int i = 0; i < 4; i ++ ){
        int x=t.first+dx[i],y=t.second+dy[i];
        if(x>=0 && x<n && y>=0 && y<m && g[x][y]==0 && d[x][y]==-1){
            d[x][y]=d[t.first][t.second]+1;
            q.push({x,y});
        }
    }
}
return d[n-1][m-1];

}

int main(){
cin >> n >>m;
for (int i = 0; i < n; i )
for (int j = 0; j < m; j
)
cin >> g[i][j];
cout << bfs();
return 0;

}



活动打卡代码 AcWing 847. 图中点的层次

Hugh.
3天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

using namespace std;

const int N = 100010,M = 100010;

int n,m;
int h[N], e[M], ne[M], idx;
int d[N];

void add(int a, int b) // 添加一条边a->b
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

int bfs(){
memset(d, -1, sizeof d);

queue<int> q;
d[1] = 0;
q.push(1);

while (q.size()){
    int t=q.front();
    q.pop();
    for (int i = h[t]; i!=-1; i=ne[i] ){
        int j=e[i];
        if(d[j]==-1){
            d[j]=d[t]+1;
            q.push(j);
        }
    }
}

return d[n];

}

int main()
{
scanf(“%d%d”, &n, &m);
memset(h, -1, sizeof h);
for (int i = 0; i < m; i ++ ){
int a,b;
scanf(“%d%d”, &a, &b);
add(a, b);
}
cout << bfs();
}



活动打卡代码 AcWing 846. 树的重心

Hugh.
3天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

using namespace std;

const int N= 100010,M = 100010*2;
int h[N], e[M], ne[M], idx;
int ans=N;
bool st[N];
int n;

void add(int a, int b) // 添加一条边a->b
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

int dfs(int u){

st[u]=true;

int sum=1,res=0;
for (int i = h[u]; i!=-1; i=ne[i]){
    int j=e[i];
    if(!st[j]){
        int s=dfs(j);
        res=max(res,s);
        sum+=s;
    }
}
res = max(res, n - sum);
ans = min(ans,res);    
return sum;

}

int main()
{
cin >> n;
memset(h, -1, sizeof h);
for (int i = 0; i < n-1; i ++ ) {
int a,b;
cin >> a >>b;
add(a, b);
add(b, a);
}
dfs(1);
cout << ans;
}



活动打卡代码 AcWing 823. 排列

Hugh.
4天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

using namespace std;

int n,path[11];
bool pd[11];
void dfs(int u){
if(u==n){
for (int i = 0; i < n; i ++ ){
cout << path[i] <<’ ‘;
}
cout << endl;
return;
}

for (int i = 1; i <= n; i ++ ){
    if(!pd[i]){
        path[u]=i;
        pd[i]=true;
        dfs(u+1);
        pd[i]=false;

    }
}

}

int main()
{
cin >> n;
dfs(0);

}




Hugh.
4天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

/
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode
next;
* ListNode(int x) : val(x), next(NULL) {}
* };
/
class Solution {
public:
vector[HTML_REMOVED] printListReversingly(ListNode* head) {
vector[HTML_REMOVED] res;
while (head) {
res.push_back(head->val);
head = head->next;
}
return vector[HTML_REMOVED](res.rbegin(), res.rend());
}
};




Hugh.
4天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

x=x-lowbit(x)



活动打卡代码 AcWing 842. 排列数字

Hugh.
4天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

using namespace std;

int n;
int g[11];
bool col[11];

void dfs(int u){
if(u==n){
for (int i = 0; i < n; i ) cout <<g[i]<<’ ‘;
cout << endl;
return;
}
for (int i = 1; i <= n; i
)
if (!col[i])
{
g[u] = i;
col[i] = true;
dfs(u + 1);
col[i] = false;
}
}

int main()
{

cin >> n;

dfs(0);

}



活动打卡代码 AcWing 843. n-皇后问题

Hugh.
4天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

include [HTML_REMOVED]

include [HTML_REMOVED]

using namespace std;

int n;
char g[11][11];
bool col[11],dg[11],udg[11];

void dfs(int u){
if(u==n){
for (int i = 0; i < n; i ) puts(g[i]);
cout << endl;
return;
}
for (int i = 0; i < n; i
)
if (!col[i] && !dg[u + i] && !udg[n - u + i])
{
g[u][i] = ‘Q’;
col[i] = dg[u + i] = udg[n - u + i] = true;
dfs(u + 1);
col[i] = dg[u + i] = udg[n - u + i] = false;
g[u][i] = ‘.’;
}
}

int main()
{

cin >> n;
    for (int i = 0; i < n; i ++ )
    for (int j = 0; j < n; j ++ )
        g[i][j] = '.';

dfs(0);

}