Hugh.

672

zhuzuojun

acmdyh
ExcenFun
Pluviophile

Pr
IAG丶Skipper

wwww_wwww

william-luo

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


# 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]

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;
d[b]
;
}

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

}


}

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


# 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;

}

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


# 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);
}
cout << bfs();
}

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


# 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;
}
dfs(1);
cout << ans;
}

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


# 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天前
//这里填你的代码^^
//注意代码要放在两组三个点之间，才可以正确显示代码高亮哦~


/
* struct ListNode {
* int val;
* ListNode
next;
* ListNode(int x) : val(x), next(NULL) {}
* };
/
class Solution {
public:
vector[HTML_REMOVED] res;
}
return vector[HTML_REMOVED](res.rbegin(), res.rend());
}
};

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


x=x-lowbit(x)

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


# 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);


}

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


# 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);


}